allow map lookups as operands of bitwise binops
cover allow map lookups as operands of bitwise binops
Hi,
this series allows a map lookup as operand of the bitwise operations &, | and ^ in statement expressions. This sets part of the packet mark from a map and keeps the other bits:
meta mark set meta mark & 0xffffff80 | (ip daddr . ip saddr map @m)
Today this takes a chain per map value, each running "meta mark set meta mark & 0xffffff80 | <value>", or a detour through the conntrack mark:
ct mark set ip daddr . ip saddr map @m
meta mark set meta mark & 0xffffff80 | ct mark
The kernel side exists already: since Linux 6.13, the bitwise expression operates on two registers, and nft uses this for variable right-hand operands since commit 54bfc38c ("src: allow binop expressions with variable right-hand operands"). Only the parsers rejected map lookups as operands.
Patch 1 changes the bison and JSON parsers, evaluation and printing. Patch 2 adds tests/py cases. Patch 3 adds tests/shell cases, including a packetpath test that checks the resulting mark on map hit and miss. The shell tests require the bitwise_multireg feature.
Two points worth a look in review:
- expr_evaluate_map() flags every map lookup as constant, and statements rely on this to skip byteorder conversion. Instead of changing that, patch 1 clears the flag on binop operands only.
- The grammar change also admits a map lookup as concatenation component. That linearized, but "nft list" then hit an assertion in netlink_parse_concat_expr(). Patch 1 rejects it in the evaluator.
Jochen Seeber (3):
src: allow map lookups as operands of bitwise binops
tests: py: add map lookups as bitwise operands
tests: shell: add map lookups as bitwise operands
doc/statements.txt | 7 + src/evaluate.c | 26 +- src/expression.c | 2 + src/parser_bison.y | 4 + src/parser_json.c | 15 +- tests/py/ip/sets.t | 2 + tests/py/ip/sets.t.json | 91 +++++++ tests/py/ip/sets.t.payload.inet | 23 ++ tests/py/ip/sets.t.payload.ip | 19 ++ tests/py/ip/sets.t.payload.netdev | 23 ++ .../shell/testcases/bitwise/0040mark_binop_14 | 14 + .../shell/testcases/bitwise/0040mark_binop_15 | 13 + .../bitwise/dumps/0040mark_binop_14.json-nft | 100 +++++++ .../bitwise/dumps/0040mark_binop_14.nft | 11 + .../bitwise/dumps/0040mark_binop_15.json-nft | 85 ++++++ .../bitwise/dumps/0040mark_binop_15.nft | 6 + .../testcases/packetpath/bitwise_map_operand | 62 +++++ .../dumps/bitwise_map_operand.json-nft | 250 ++++++++++++++++++ .../packetpath/dumps/bitwise_map_operand.nft | 19 ++ 19 files changed, 768 insertions(+), 4 deletions(-) create mode 100755 tests/shell/testcases/bitwise/0040mark_binop_14 create mode 100755 tests/shell/testcases/bitwise/0040mark_binop_15 create mode 100644 tests/shell/testcases/bitwise/dumps/0040mark_binop_14.json-nft create mode 100644 tests/shell/testcases/bitwise/dumps/0040mark_binop_14.nft create mode 100644 tests/shell/testcases/bitwise/dumps/0040mark_binop_15.json-nft create mode 100644 tests/shell/testcases/bitwise/dumps/0040mark_binop_15.nft create mode 100755 tests/shell/testcases/packetpath/bitwise_map_operand create mode 100644 tests/shell/testcases/packetpath/dumps/bitwise_map_operand.json-nft create mode 100644 tests/shell/testcases/packetpath/dumps/bitwise_map_operand.nft
Signature · 1 line
2.56.0
PATCH 1/3 src: allow map lookups as operands of bitwise binops
Since Linux 6.13, the kernel performs AND, OR and XOR on two registers, and nft accepts variable right-hand operands such as:
ct mark set ct mark & 0xffff0000 | meta mark & 0xffff
A map lookup could not be such an operand, though. Setting part of the packet mark from a map took a chain per map value, or a detour through the conntrack mark.
Accept a map lookup enclosed in parentheses as operand of a binop in statement expressions, e.g.:
meta mark set meta mark & 0xffffff80 | (ip daddr . ip saddr map @m)
which yields:
[ meta load mark => reg 1 ]
[ bitwise reg 1 = ( reg 1 & 0xffffff80 ) ^ 0x00000000 ] [ payload load 4b @ network header + 16 => reg 2 ] [ payload load 4b @ network header + 12 => reg 13 ] [ lookup reg 2 set m dreg 2 ]
[ bitwise reg 1 = ( reg 1 | reg 2 ) ]
[ meta set mark with reg 1 ]
The parentheses are required: without them, the existing grammar takes "meta mark & 0xffffff80 | ip daddr . ip saddr" as the map key.
The evaluator flags map lookups as constant, so that statements use the map data as is. Clear the flag on binop operands, so that they take the register-to-register path, and keep the statement length, which map evaluation resets. List map operands in parentheses, so that the output parses back.
The new grammar rule also admits a map lookup as concatenation component, which linearizes but cannot be listed. Reject it in the evaluator.
In JSON, accept a map as right-hand binop operand in statement context and parse map key and data independently of the operand position.
This requires Linux kernel >= 6.13.
--- doc/statements.txt | 7 +++++++ src/evaluate.c | 26 +++++++++++++++++++++++--- src/expression.c | 2 ++ src/parser_bison.y | 4 ++++ src/parser_json.c | 15 ++++++++++++++- 5 files changed, 50 insertions(+), 4 deletions(-)
doc/statements.txt +7 -0
@@ -932,6 +932,9 @@ ____ The 'key' is a value returned by 'expression'. // XXX: Write about where map statement can be used (list of statements?) +Enclosed in parentheses, a map lookup can also be an operand of the binary +operations *&*, *|* and *^*. This requires Linux kernel 6.13 or later. + .Using the map statement ------------------------ # select DNAT target based on TCP dport:
@@ -943,6 +946,10 @@ nft add rule ip nat prerouting dnat tcp dport map { 80 : 192.168.1.100, 8888 : 1 # packets from net 192.168.1.0/24 will appear as originating from 10.0.0.1, # packets from net 192.168.2.0/24 will appear as originating from 10.0.0.2 nft add rule ip nat postrouting snat to ip saddr map { 192.168.1.0/24 : 10.0.0.1, 192.168.2.0/24 : 10.0.0.2 } + +# replace the lower 7 bits of the packet mark by the value mapped to the +# destination and source address, keep the other bits +nft add rule ip mangle output 'meta mark set meta mark and 0xffffff80 or (ip daddr . ip saddr map @marks)' ------------------------ VMAP STATEMENT
src/evaluate.c +23 -3
@@ -1522,6 +1522,26 @@ static int expr_evaluate_bitwise(struct eval_ctx *ctx, struct expr **expr)return 0 ; } +/* +* Map lookups are flagged constant so that statements use the map data as is. +* As a binop operand, a map lookup is a value only known at runtime. +*/ +static int expr_evaluate_binop_operand (struct eval_ctx *ctx, +struct expr **operand) +{ +uint32_t stmt_len = ctx->stmt_len; + +if (expr_evaluate (ctx, operand) <0 ) +return -1 ; + +if ((*operand)->etype ==EXPR_MAP ) { + (*operand)->flags &= ~EXPR_F_CONSTANT ; + ctx->stmt_len = stmt_len; + } + +return 0 ; +} +/* * Binop expression: both sides must be of integer base type. The left-hand side * may be either constant or non-constant; if it is constant, it must be a
@@ -1543,7 +1563,7 @@ static int expr_evaluate_binop(struct eval_ctx *ctx, struct expr **expr) ctx->recursion.binop); ctx->recursion.binop++; -if (expr_evaluate (ctx, &op->left) <0 ) +if (expr_evaluate_binop_operand (ctx, &op->left) <0 )return -1 ; left = op->left;
@@ -1555,7 +1575,7 @@ static int expr_evaluate_binop(struct eval_ctx *ctx, struct expr **expr) left->byteorder, max_shift_len, 0); } -if (expr_evaluate (ctx, &op->right) <0 ) +if (expr_evaluate_binop_operand (ctx, &op->right) <0 )return -1 ; right = op->right;
@@ -1741,7 +1761,6 @@ static int expr_evaluate_concat(struct eval_ctx *ctx, struct expr **expr)case EXPR_BINOP :case EXPR_RELATIONAL :case EXPR_CONCAT : -case EXPR_MAP :case EXPR_PAYLOAD :case EXPR_EXTHDR :case EXPR_META :
@@ -1780,6 +1799,7 @@ static int expr_evaluate_concat(struct eval_ctx *ctx, struct expr **expr)case EXPR_LIST :case EXPR_SET :case EXPR_SET_REF : +case EXPR_MAP :case EXPR_MAPPING :case EXPR_SET_ELEM_CATCHALL :case EXPR_RANGE_SYMBOL :
src/expression.c +2 -0
@@ -839,6 +839,8 @@ static void binop_arg_print(const struct expr *op, const struct expr *arg, expr_binop_precedence[op->op] != 0 && expr_binop_precedence[op->op] < expr_binop_precedence[arg->op]) prec =1 ; + else if (arg->etype == EXPR_MAP) + prec =1 ;if (prec)nft_print (octx,"(" );
src/parser_bison.y +4 -0
@@ -4000,6 +4000,10 @@ primary_stmt_expr : symbol_expr { $$ = $1; } | fib_expr { $$ = $1; } | osf_expr { $$ = $1; } | '(' basic_stmt_expr ')' { $$ = $2; } + | '(' concat_stmt_expr MAP map_stmt_expr_set ')' + { + $$ = map_expr_alloc(&@$, $2, $4); + } ; shift_stmt_expr : primary_stmt_expr
src/parser_json.c +14 -1
@@ -1533,20 +1533,33 @@ static struct expr *json_parse_set_expr(struct json_ctx *ctx,static struct expr *json_parse_map_expr (struct json_ctx *ctx,const char *type,json_t *root) { +uint32_t old_flags = ctx->flags;json_t *jkey, *jdata;struct expr *key, *data; +/* on the RHS, a map is only valid as binop operand in a statement */ +if (is_RHS (ctx) && !is_STMT (ctx)) { +json_error (ctx,"Expression type map not allowed in context (%s)." , +ctx_flags_to_string (ctx)); +return NULL ; + } +if (json_unpack_err (ctx, root,"{s:o, s:o}" ,"key" , &jkey,"data" , &jdata))return NULL ; +/* key and data do not depend on the map being a binop operand */ + ctx->flags &= ~(CTX_F_PRIMARY |CTX_F_RHS ); + key =json_parse_map_lhs_expr (ctx, jkey);if (!key) {json_error (ctx,"Illegal map expression key." ); + ctx->flags = old_flags;return NULL ; } data =json_parse_rhs_expr (ctx, jdata); + ctx->flags = old_flags;if (!data) {json_error (ctx,"Illegal map expression data." );expr_free (key);
@@ -1652,7 +1665,7 @@ static struct expr *json_parse_expr(struct json_ctx *ctx, json_t *root) } cb_tbl[] = { { "concat", json_parse_concat_expr, CTX_F_RHS | CTX_F_STMT | CTX_F_DTYPE | CTX_F_SET_RHS | CTX_F_SES | CTX_F_MAP }, { "set", json_parse_set_expr, CTX_F_RHS | CTX_F_STMT }, /* allow this as stmt expr because that allows set references */ - { "map", json_parse_map_expr, CTX_F_STMT | CTX_F_PRIMARY | CTX_F_SET_RHS }, + { "map", json_parse_map_expr, CTX_F_RHS | CTX_F_STMT | CTX_F_PRIMARY | CTX_F_SET_RHS },/* below three are multiton_rhs_expr */ { "prefix", json_parse_prefix_expr, CTX_F_RHS | CTX_F_SET_RHS | CTX_F_STMT | CTX_F_CONCAT }, { "range", json_parse_range_expr, CTX_F_RHS | CTX_F_SET_RHS | CTX_F_STMT | CTX_F_CONCAT },
Signature · 1 line
2.56.0
PATCH 2/3 tests: py: add map lookups as bitwise operands
In reply to Jochen SeeberCover a named map with concatenated key as right-hand and left-hand operand of bitwise operations on the packet mark.
--- tests/py/ip/sets.t | 2 + tests/py/ip/sets.t.json | 91 +++++++++++++++++++++++++++++++ tests/py/ip/sets.t.payload.inet | 23 ++++++++ tests/py/ip/sets.t.payload.ip | 19 +++++++ tests/py/ip/sets.t.payload.netdev | 23 ++++++++ 5 files changed, 158 insertions(+)
tests/py/ip/sets.t +2 -0
@@ -54,6 +54,8 @@ add @set5 { ip saddr . ip daddr };ok !map1 type ipv4_addr . ipv4_addr : mark;ok add @map1 { ip saddr . ip daddr : meta mark };ok +meta mark set meta mark & 0xffffff80 | (ip saddr . ip daddr map @map1);ok +meta mark set (ip saddr . ip daddr map @map1) ^ meta mark;ok # test nested anonymous sets ip saddr { { 1.1.1.0, 3.3.3.0 }, 2.2.2.0 };ok;ip saddr { 1.1.1.0, 2.2.2.0, 3.3.3.0 }
tests/py/ip/sets.t.json +91 -0
97 lines not on this page · raw message
tests/py/ip/sets.t.payload.inet +23 -0
@@ -84,6 +84,29 @@ inet test-inet input [ meta load mark => reg 10 ] [ dynset add reg_key 1 set map1 sreg_data 10 ] +# meta mark set meta mark & 0xffffff80 | (ip saddr . ip daddr map @map1) +inet test-inet input + [ meta load nfproto => reg 1 ] + [ cmp eq reg 1 0x02 ] + [ meta load mark => reg 1 ] + [ bitwise reg 1 = ( reg 1 & 0xffffff80 ) ^ 0x00000000 ] + [ payload load 4b @ network header + 12 => reg 2 ] + [ payload load 4b @ network header + 16 => reg 13 ] + [ lookup reg 2 set map1 dreg 2 ] + [ bitwise reg 1 = ( reg 1 | reg 2 ) ] + [ meta set mark with reg 1 ] + +# meta mark set (ip saddr . ip daddr map @map1) ^ meta mark +inet test-inet input + [ meta load nfproto => reg 1 ] + [ cmp eq reg 1 0x02 ] + [ payload load 4b @ network header + 12 => reg 1 ] + [ payload load 4b @ network header + 16 => reg 9 ] + [ lookup reg 1 set map1 dreg 1 ] + [ meta load mark => reg 2 ] + [ bitwise reg 1 = ( reg 1 ^ reg 2 ) ] + [ meta set mark with reg 1 ] + # ip saddr vmap { 1.1.1.1 : drop, * : accept } __map%d test-inet b __map%d test-inet 0
tests/py/ip/sets.t.payload.ip +19 -0
@@ -81,6 +81,25 @@ ip test-ip4 input [ meta load mark => reg 10 ] [ dynset add reg_key 1 set map1 sreg_data 10 ] +# meta mark set meta mark & 0xffffff80 | (ip saddr . ip daddr map @map1) +ip test-ip4 input + [ meta load mark => reg 1 ] + [ bitwise reg 1 = ( reg 1 & 0xffffff80 ) ^ 0x00000000 ] + [ payload load 4b @ network header + 12 => reg 2 ] + [ payload load 4b @ network header + 16 => reg 13 ] + [ lookup reg 2 set map1 dreg 2 ] + [ bitwise reg 1 = ( reg 1 | reg 2 ) ] + [ meta set mark with reg 1 ] + +# meta mark set (ip saddr . ip daddr map @map1) ^ meta mark +ip test-ip4 input + [ payload load 4b @ network header + 12 => reg 1 ] + [ payload load 4b @ network header + 16 => reg 9 ] + [ lookup reg 1 set map1 dreg 1 ] + [ meta load mark => reg 2 ] + [ bitwise reg 1 = ( reg 1 ^ reg 2 ) ] + [ meta set mark with reg 1 ] + # add @map2 { ip saddr . ip daddr . th dport : 10.0.0.1 . 80 } ip test-ip4 input [ payload load 4b @ network header + 12 => reg 1 ]
tests/py/ip/sets.t.payload.netdev +23 -0
@@ -105,6 +105,29 @@ netdev test-netdev ingress [ meta load mark => reg 10 ] [ dynset add reg_key 1 set map1 sreg_data 10 ] +# meta mark set meta mark & 0xffffff80 | (ip saddr . ip daddr map @map1) +netdev test-netdev ingress + [ meta load protocol => reg 1 ] + [ cmp eq reg 1 0x0800 ] + [ meta load mark => reg 1 ] + [ bitwise reg 1 = ( reg 1 & 0xffffff80 ) ^ 0x00000000 ] + [ payload load 4b @ network header + 12 => reg 2 ] + [ payload load 4b @ network header + 16 => reg 13 ] + [ lookup reg 2 set map1 dreg 2 ] + [ bitwise reg 1 = ( reg 1 | reg 2 ) ] + [ meta set mark with reg 1 ] + +# meta mark set (ip saddr . ip daddr map @map1) ^ meta mark +netdev test-netdev ingress + [ meta load protocol => reg 1 ] + [ cmp eq reg 1 0x0800 ] + [ payload load 4b @ network header + 12 => reg 1 ] + [ payload load 4b @ network header + 16 => reg 9 ] + [ lookup reg 1 set map1 dreg 1 ] + [ meta load mark => reg 2 ] + [ bitwise reg 1 = ( reg 1 ^ reg 2 ) ] + [ meta set mark with reg 1 ] + # add @map2 { ip saddr . ip daddr . th dport : 10.0.0.1 . 80 } netdev test-netdev ingress [ meta load protocol => reg 1 ]
Signature · 1 line
2.56.0
PATCH 3/3 tests: shell: add map lookups as bitwise operands
In reply to Jochen SeeberAdd parser tests for a named interval map with concatenated key and for an anonymous map as binop operands of mark statements.
Add a packetpath test which checks that a map hit replaces the masked bits of the packet mark and keeps the others, and that a map miss leaves the mark alone.
--- .../shell/testcases/bitwise/0040mark_binop_14 | 14 + .../shell/testcases/bitwise/0040mark_binop_15 | 13 + .../bitwise/dumps/0040mark_binop_14.json-nft | 100 +++++++ .../bitwise/dumps/0040mark_binop_14.nft | 11 + .../bitwise/dumps/0040mark_binop_15.json-nft | 85 ++++++ .../bitwise/dumps/0040mark_binop_15.nft | 6 + .../testcases/packetpath/bitwise_map_operand | 62 +++++ .../dumps/bitwise_map_operand.json-nft | 250 ++++++++++++++++++ .../packetpath/dumps/bitwise_map_operand.nft | 19 ++ 9 files changed, 560 insertions(+) create mode 100755 tests/shell/testcases/bitwise/0040mark_binop_14 create mode 100755 tests/shell/testcases/bitwise/0040mark_binop_15 create mode 100644 tests/shell/testcases/bitwise/dumps/0040mark_binop_14.json-nft create mode 100644 tests/shell/testcases/bitwise/dumps/0040mark_binop_14.nft create mode 100644 tests/shell/testcases/bitwise/dumps/0040mark_binop_15.json-nft create mode 100644 tests/shell/testcases/bitwise/dumps/0040mark_binop_15.nft create mode 100755 tests/shell/testcases/packetpath/bitwise_map_operand create mode 100644 tests/shell/testcases/packetpath/dumps/bitwise_map_operand.json-nft create mode 100644 tests/shell/testcases/packetpath/dumps/bitwise_map_operand.nft
tests/shell/testcases/bitwise/0040mark_binop_14 new file +14 -0
@@ -0,0 +1,14 @@ +#!/bin/bash + +# NFT_TEST_REQUIRES(NFT_TEST_HAVE_bitwise_multireg) + +set -e + +RULESET=" + add table t + add chain t c { type filter hook output priority filter; } + add map t m { type ipv4_addr . ipv4_addr : mark; flags interval; } + add rule t c meta mark set meta mark and 0xffffff80 or (ip daddr . ip saddr map @m) +" + +$NFT -f - <<< "$RULESET"
tests/shell/testcases/bitwise/0040mark_binop_15 new file +13 -0
@@ -0,0 +1,13 @@ +#!/bin/bash + +# NFT_TEST_REQUIRES(NFT_TEST_HAVE_bitwise_multireg) + +set -e + +RULESET=" + add table t + add chain t c { type filter hook output priority filter; } + add rule t c ct mark set (ip daddr map { 10.0.0.1 : 0x1, 10.0.0.2 : 0x2 }) or ct mark and 0xfffffffc +" + +$NFT -f - <<< "$RULESET"
tests/shell/testcases/bitwise/dumps/0040mark_binop_14.json-nft new file +100 -0
100 lines not on this page · raw message
tests/shell/testcases/bitwise/dumps/0040mark_binop_14.nft new file +11 -0
@@ -0,0 +1,11 @@ +table ip t { + map m { + type ipv4_addr . ipv4_addr : mark + flags interval + } + + chain c { + type filter hook output priority filter; policy accept; + meta mark set meta mark & 0xffffff80 | (ip daddr . ip saddr map @m) + } +}
tests/shell/testcases/bitwise/dumps/0040mark_binop_15.json-nft new file +85 -0
85 lines not on this page · raw message
tests/shell/testcases/bitwise/dumps/0040mark_binop_15.nft new file +6 -0
@@ -0,0 +1,6 @@ +table ip t { + chain c { + type filter hook output priority filter; policy accept; + ct mark set (ip daddr map { 10.0.0.1 : 0x00000001, 10.0.0.2 : 0x00000002 }) | ct mark & 0xfffffffc + } +}
tests/shell/testcases/packetpath/bitwise_map_operand new file +62 -0
62 lines not on this page · raw message
tests/shell/testcases/packetpath/dumps/bitwise_map_operand.json-nft new file +250 -0
250 lines not on this page · raw message
tests/shell/testcases/packetpath/dumps/bitwise_map_operand.nft new file +19 -0
@@ -0,0 +1,19 @@ +table ip test-binop { + map m { + type ipv4_addr . ipv4_addr : mark + flags interval + elements = { 127.0.0.1 . 127.0.0.0/8 : 0x00000003 } + } + + chain in { + type filter hook input priority filter; policy accept; + icmp type echo-request meta mark 0x00004003 counter packets 1 bytes 84 + icmp type echo-request meta mark 0x00004005 counter packets 1 bytes 84 + } + + chain out { + type filter hook output priority filter; policy accept; + icmp type echo-request meta mark set 0x00004005 + icmp type echo-request meta mark set meta mark & 0xffffff80 | (ip daddr . ip saddr map @m) + } +}
Signature · 1 line
2.56.0