json: Accept more than two operands in binary expressions
Phil Sutter
reviewerv2 cover json: Accept more than two operands in binary expressions
This needed rebase of patch 1 to cover for intermediate changes got a bit out of hand and resulted in this series:
Patch 2 fixes a bug in set element sorting triggered by binop expression elements being treated equal in value if the LHS parts match.
Patch 3 collates general bugs in recorded JSON equivalents for py testsuite.
Patch 4 adds detection of needless recorded JSON outputs to nft-tests.py, patch 5 then performs the cleanup to eliminate the warnings it generates.
Phil Sutter (5):
json: Accept more than two operands in binary expressions
mergesort: Avoid accidental set element reordering
tests: py: Fix some JSON equivalents
tests: py: Warn if recorded JSON output matches the input
tests: py: Drop needless recorded JSON outputs
doc/libnftables-json.adoc | 18 +- src/intervals.c | 2 +- src/json.c | 19 +- src/mergesort.c | 2 +- src/parser_json.c | 12 + tests/py/any/last.t.json.output | 7 - tests/py/any/meta.t.json | 2 +- tests/py/any/meta.t.json.output | 180 ---------- tests/py/any/tcpopt.t.json | 4 +- tests/py/inet/tcp.t.json | 189 ++++------ tests/py/inet/tcp.t.json.output | 339 +----------------- tests/py/ip/numgen.t.json.output | 30 -- tests/py/ip6/exthdr.t.json.output | 30 -- tests/py/nft-test.py | 2 + .../dumps/0012different_defines_0.json-nft | 8 +- .../sets/dumps/0055tcpflags_0.json-nft | 114 ++---- .../testcases/sets/dumps/0055tcpflags_0.nft | 8 +- 17 files changed, 166 insertions(+), 800 deletions(-)
Signature · 1 line
2.43.0
Phil Sutter
reviewerPATCH v2 1/5 json: Accept more than two operands in binary expressions
The most common use case is ORing flags like
| syn | ack | rst
but nft seems to be fine with less intuitive stuff like
| meta mark set ip dscp << 2 << 3
so support all of them.
--- doc/libnftables-json.adoc | 18 +-- src/json.c | 19 +++- src/parser_json.c | 12 ++ tests/py/inet/tcp.t.json | 67 +---------- tests/py/inet/tcp.t.json.output | 104 ++++-------------- .../dumps/0012different_defines_0.json-nft | 8 +- .../sets/dumps/0055tcpflags_0.json-nft | 98 ++++------------- 7 files changed, 91 insertions(+), 235 deletions(-)
doc/libnftables-json.adoc +10 -8
@@ -1352,15 +1352,17 @@ Perform kernel Forwarding Information Base lookups. === BINARY OPERATION [verse] -*{ "|": [* 'EXPRESSION'*,* 'EXPRESSION' *] }* -*{ "^": [* 'EXPRESSION'*,* 'EXPRESSION' *] }* -*{ "&": [* 'EXPRESSION'*,* 'EXPRESSION' *] }* -*{ "+<<+": [* 'EXPRESSION'*,* 'EXPRESSION' *] }* -*{ ">>": [* 'EXPRESSION'*,* 'EXPRESSION' *] }* - -All binary operations expect an array of exactly two expressions, of which the +*{ "|": [* 'EXPRESSION'*,* 'EXPRESSIONS' *] }* +*{ "^": [* 'EXPRESSION'*,* 'EXPRESSIONS' *] }* +*{ "&": [* 'EXPRESSION'*,* 'EXPRESSIONS' *] }* +*{ "+<<+": [* 'EXPRESSION'*,* 'EXPRESSIONS' *] }* +*{ ">>": [* 'EXPRESSION'*,* 'EXPRESSIONS' *] }* +'EXPRESSIONS' := 'EXPRESSION' | 'EXPRESSION'*,* 'EXPRESSIONS' + +All binary operations expect an array of at least two expressions, of which the first element denotes the left hand side and the second one the right hand -side. +side. Extra elements are accepted in the given array and appended to the term +accordingly. === VERDICT [verse]
src/json.c +16 -3
@@ -540,11 +540,24 @@ json_t *flagcmp_expr_json(const struct expr *expr, struct output_ctx *octx) "right", expr_print_json(expr->flagcmp.value, octx)); } +static json_t * +__binop_expr_json (int op,const struct expr *expr,struct output_ctx *octx) +{ +json_t *a =json_array (); + +if (expr->etype ==EXPR_BINOP && expr->op == op) { +json_array_extend (a,__binop_expr_json (op, expr->left, octx)); +json_array_extend (a,__binop_expr_json (op, expr->right, octx)); + }else { +json_array_append_new (a,expr_print_json (expr, octx)); + } +return a; +} +json_t *binop_expr_json (const struct expr *expr,struct output_ctx *octx) { -return json_pack ("{s:[o, o]}" , expr_op_symbols[expr->op], -expr_print_json (expr->left, octx), -expr_print_json (expr->right, octx)); +return json_pack ("{s:o}" , expr_op_symbols[expr->op], +__binop_expr_json (expr->op, expr, octx)); }json_t *relational_expr_json (const struct expr *expr,struct output_ctx *octx)
src/parser_json.c +12 -0
@@ -1204,6 +1204,18 @@ static struct expr *json_parse_binop_expr(struct json_ctx *ctx,return NULL ; } +if (json_array_size (root) >2 ) { + left =json_parse_primary_expr (ctx,json_array_get (root,0 )); + right =json_parse_primary_expr (ctx,json_array_get (root,1 )); + left =binop_expr_alloc (int_loc, thisop, left, right); +for (i =2 ; i <json_array_size (root); i++) { + jright =json_array_get (root, i); + right =json_parse_primary_expr (ctx, jright); + left =binop_expr_alloc (int_loc, thisop, left, right); + } +return left; + } +if (json_unpack_err (ctx, root,"[o, o!]" , &jleft, &jright))return NULL ;
tests/py/inet/tcp.t.json +6 -61
95 lines not on this page · raw message
tests/py/inet/tcp.t.json.output +22 -82
146 lines not on this page · raw message
tests/shell/testcases/nft-f/dumps/0012different_defines_0.json-nft +2 -6
@@ -169,12 +169,8 @@ }, "right": { "|": [ - { - "|": [ - "established", - "related" - ] - }, + "established", + "related", "new" ] }
tests/shell/testcases/sets/dumps/0055tcpflags_0.json-nft +23 -75
152 lines not on this page · raw message
Signature · 1 line
2.43.0
Phil Sutter
reviewerPATCH v2 2/5 mergesort: Avoid accidental set element reordering
In reply to Phil SutterIn corner cases, expr_msort_cmp() may return 0 for two non-identical elements. An example are ORed tcp flags: 'syn' and 'syn | ack' are considered the same value since expr_msort_value() reduces the latter to its LHS.
Keeping the above in mind and looking at how list_expr_sort() works: The list in 'head' is cut in half, the first half put into the temporary list 'list' and finally 'list' is merged back into 'head' considering each element's position. Shall expr_msort_cmp() return 0 for two elements, the one from 'list' ends up after the one in 'head', thus reverting their previous ordering.
The practical implication is that output never matches input for the sample set '{ syn, syn | ack }' as the sorting after delinearization in netlink_list_setelems() keeps swapping the elements. Out of coincidence, the commit this fixes itself illustrates the use-case this breaks, namely tracking a ruleset in git: Each ruleset reload will trigger an update to the stored dump.
This change breaks interval set element deletion because __set_delete() implicitly relies upon this reordering of duplicate entries by inserting a clone of the one to delete into the start (via list_move()) and after sorting assumes the clone will end up right behind the original. Fix this by calling list_move_tail() instead.
--- src/intervals.c | 2 +- src/mergesort.c | 2 +- .../sets/dumps/0055tcpflags_0.json-nft | 32 +++++++++---------- .../testcases/sets/dumps/0055tcpflags_0.nft | 8 ++--- 4 files changed, 22 insertions(+), 22 deletions(-)
src/intervals.c +1 -1
@@ -466,7 +466,7 @@ static int __set_delete(struct list_head *msgs, struct expr *i, struct set *set, unsigned int debug_mask) { i->flags |=EXPR_F_REMOVE ; -list_move (&i->list, &existing_set->init->expressions); +list_move_tail (&i->list, &existing_set->init->expressions);list_expr_sort (&existing_set->init->expressions);return setelem_delete (msgs, set, init, existing_set->init, debug_mask);
src/mergesort.c +1 -1
@@ -78,7 +78,7 @@ void list_splice_sorted(struct list_head *list, struct list_head *head)while (l != list) {if (h == head || expr_msort_cmp(list_entry(l, typeof(struct expr), list), - list_entry(h, typeof(struct expr), list)) < 0) { + list_entry(h, typeof(struct expr), list)) <= 0) { l = l->next;list_add_tail (l->prev, h);continue ;
tests/shell/testcases/sets/dumps/0055tcpflags_0.json-nft +16 -16
@@ -28,15 +28,6 @@ { "|": [ "fin", - "psh", - "ack", - "urg" - ] - }, - { - "|": [ - "fin", - "psh", "ack" ] },
@@ -50,21 +41,22 @@ { "|": [ "fin", + "psh", "ack" ] }, { "|": [ - "syn", + "fin", "psh", "ack", "urg" ] }, + "syn", { "|": [ "syn", - "psh", "ack" ] },
@@ -78,22 +70,22 @@ { "|": [ "syn", + "psh", "ack" ] }, - "syn", { "|": [ - "rst", + "syn", "psh", "ack", "urg" ] }, + "rst", { "|": [ "rst", - "psh", "ack" ] },
@@ -107,12 +99,13 @@ { "|": [ "rst", + "psh", "ack" ] }, - "rst", { "|": [ + "rst", "psh", "ack", "urg"
@@ -126,11 +119,18 @@ }, { "|": [ + "psh", "ack", "urg" ] }, - "ack" + "ack", + { + "|": [ + "ack", + "urg" + ] + } ] } }
tests/shell/testcases/sets/dumps/0055tcpflags_0.nft +4 -4
@@ -2,9 +2,9 @@ table ip test { set tcp_good_flags { type tcp_flag flags constant - elements = { fin | psh | ack | urg, fin | psh | ack, fin | ack | urg, fin | ack, syn | psh | ack | urg, - syn | psh | ack, syn | ack | urg, syn | ack, syn, rst | psh | ack | urg, - rst | psh | ack, rst | ack | urg, rst | ack, rst, psh | ack | urg, - psh | ack, ack | urg, ack } + elements = { fin | ack, fin | ack | urg, fin | psh | ack, fin | psh | ack | urg, syn, + syn | ack, syn | ack | urg, syn | psh | ack, syn | psh | ack | urg, rst, + rst | ack, rst | ack | urg, rst | psh | ack, rst | psh | ack | urg, psh | ack, + psh | ack | urg, ack, ack | urg } } }
Signature · 1 line
2.43.0
Phil Sutter
reviewerPATCH v2 3/5 tests: py: Fix some JSON equivalents
In reply to Phil SutterMake sure they match the standard syntax input as much as possible.
For some reason inet/tcp.t.json was using plain arrays in place of binary OR expressions in many cases. These arrays are interpreted as list expressions, which seems to be semantically identical but the goal here is to present an accurate equivalent to the rule in standard syntax.
--- tests/py/any/meta.t.json | 2 +- tests/py/any/tcpopt.t.json | 4 +- tests/py/inet/tcp.t.json | 124 +++++++++++++++++++++---------------- 3 files changed, 75 insertions(+), 55 deletions(-)
tests/py/any/meta.t.json +1 -1
@@ -2661,7 +2661,7 @@ } }, "op": "==", - "right": "17:00" + "right": "17:00:00" } }, {
tests/py/any/tcpopt.t.json +2 -2
@@ -192,7 +192,7 @@ "left": { "tcp option": { "field": "left", - "name": "sack" + "name": "sack0" } }, "op": "==",
@@ -272,7 +272,7 @@ "left": { "tcp option": { "field": "right", - "name": "sack" + "name": "sack0" } }, "op": "==",
tests/py/inet/tcp.t.json +72 -52
194 lines not on this page · raw message
Signature · 1 line
2.43.0
Phil Sutter
reviewerPATCH v2 4/5 tests: py: Warn if recorded JSON output matches the input
In reply to Phil SutterActively support spring-cleaning by nagging callers.
--- tests/py/nft-test.py | 2 ++ 1 file changed, 2 insertions(+)
tests/py/nft-test.py +2 -0
@@ -809,6 +809,8 @@ def set_delete_elements(set_element, set_name, table, filename=None, reason = "Invalid JSON syntax in expected output: %s" % json_expected print_error(reason) return [-1, warning, error, unit_tests] + if json_expected == json_input: + print_warning("Recorded JSON output matches input for: %s" % rule[0]) for table in table_list: if rule[1].strip() == "ok":
Signature · 1 line
2.43.0
Phil Sutter
reviewerPATCH v2 5/5 tests: py: Drop needless recorded JSON outputs
In reply to Phil SutterThese match the input already, no need to track them.
--- tests/py/any/last.t.json.output | 7 - tests/py/any/meta.t.json.output | 180 -------------------- tests/py/inet/tcp.t.json.output | 265 ------------------------------ tests/py/ip/numgen.t.json.output | 30 ---- tests/py/ip6/exthdr.t.json.output | 30 ---- 5 files changed, 512 deletions(-)
tests/py/any/last.t.json.output +0 -7
@@ -1,10 +1,3 @@ -# last -[ - { - "last": null - } -] - # last used 300s [ {
tests/py/any/meta.t.json.output +0 -180
201 lines not on this page · raw message
tests/py/inet/tcp.t.json.output +0 -265
274 lines not on this page · raw message
tests/py/ip/numgen.t.json.output +0 -30
@@ -80,33 +80,3 @@ } ] -# dnat to numgen inc mod 7 offset 167772161 -[ - { - "dnat": { - "addr": { - "numgen": { - "mod": 7, - "mode": "inc", - "offset": 167772161 - } - } - } - } -] - -# dnat to numgen inc mod 255 offset 167772161 -[ - { - "dnat": { - "addr": { - "numgen": { - "mod": 255, - "mode": "inc", - "offset": 167772161 - } - } - } - } -] -
tests/py/ip6/exthdr.t.json.output +0 -30
@@ -1,33 +1,3 @@ -# exthdr hbh == exists -[ - { - "match": { - "left": { - "exthdr": { - "name": "hbh" - } - }, - "op": "==", - "right": true - } - } -] - -# exthdr hbh == missing -[ - { - "match": { - "left": { - "exthdr": { - "name": "hbh" - } - }, - "op": "==", - "right": false - } - } -] - # exthdr hbh 1 [ {
Signature · 1 line
2.43.0
Phil Sutter
reviewerQuoted 13 lines · Phil Sutter
This needed rebase of patch 1 to cover for intermediate changes got a bit out of hand and resulted in this series:
Patch 2 fixes a bug in set element sorting triggered by binop expression elements being treated equal in value if the LHS parts match.
Patch 3 collates general bugs in recorded JSON equivalents for py testsuite.
Patch 4 adds detection of needless recorded JSON outputs to
nft-tests.py, patch 5 then performs the cleanup to eliminate the warnings it generates.
Quoted 6 lines
Phil Sutter (5):
json: Accept more than two operands in binary expressions
mergesort: Avoid accidental set element reordering
tests: py: Fix some JSON equivalents
tests: py: Warn if recorded JSON output matches the input
tests: py: Drop needless recorded JSON outputs
Series applied.