Bitwise boolean operations with variable RHS operands
v5 cover Bitwise boolean operations with variable RHS operands
This patch-set adds support for new bitwise boolean operations to nftables, and uses this to extend the types of value which can be assigned to packet marks and payload fields. The original motivation for these changes was Kevin Darbyshire-Bryant's wish to be able to set the conntrack mark to a bitwise expression derived from a DSCP value:
https://lore.kernel.org/netfilter-devel/[email protected]/#r
For example:
nft add rule t c ct mark set ip dscp lshift 26 or 0x10
Examples like this could be implemented solely by changes to user space. However, other examples came up in later discussion, such as:
nft add rule t c ct mark set ct mark and 0xffff0000 or meta mark and 0xffff
and most recently:
nft add rule t c ct mark set ct mark or ip dscp or 0x200
which require boolean bitwise operations with two variable operands.
Hitherto, the kernel has required that AND, OR and XOR operations be converted in user space to mask-and-xor operations on one register and two immediate values. The related kernel space patch-set, however, adds support for performing these operations directly on one register and an immediate value, or on two registers. This patch-set extends nftables to make use of this functionality.
The previous version of this series also included a few small changes to make it easier to add debug output and changes to support the assign- ments which did not require binops on two registers. The former have been dropped and the latter were reworked and applied by Pablo. The following remain.
* Patch 1 adds support for linearizing and delinearizing the new operations.
* Patches 2-7 add support for using them in payload and mark assignments.
* Patch 8 adds tests for the new assignments.
Jeremy Sowden (8):
netlink: support (de)linearization of new bitwise boolean operations
netlink_delinearize: refactor stmt_payload_binop_postprocess
netlink_delinearize: add support for processing variable payload
statement arguments
evaluate: prevent nested byte-order conversions
evaluate: preserve existing binop properties
evaluate: allow binop expressions with variable right-hand operands
parser_json: allow RHS mark and payload expressions
tests: add tests for binops with variable RHS operands
include/linux/netfilter/nf_tables.h | 19 +- src/evaluate.c | 67 ++-- src/netlink_delinearize.c | 335 ++++++++++++------ src/netlink_linearize.c | 62 +++- src/parser_json.c | 8 +- tests/py/any/ct.t | 1 + tests/py/any/ct.t.json | 37 ++ tests/py/any/ct.t.payload | 9 + tests/py/inet/meta.t | 2 + tests/py/inet/meta.t.json | 37 ++ tests/py/inet/meta.t.payload | 9 + tests/py/ip/ct.t | 1 + tests/py/ip/ct.t.json | 36 ++ tests/py/ip/ct.t.payload | 11 + tests/py/ip/ip.t | 2 + tests/py/ip/ip.t.json | 77 +++- tests/py/ip/ip.t.payload | 28 ++ tests/py/ip/ip.t.payload.bridge | 32 ++ tests/py/ip/ip.t.payload.inet | 32 ++ tests/py/ip/ip.t.payload.netdev | 32 ++ tests/py/ip6/ct.t | 1 + tests/py/ip6/ct.t.json | 36 ++ tests/py/ip6/ct.t.payload | 12 + tests/py/ip6/ip6.t | 2 + tests/py/ip6/ip6.t.json | 76 ++++ tests/py/ip6/ip6.t.payload.inet | 36 ++ tests/py/ip6/ip6.t.payload.ip6 | 32 ++ .../shell/testcases/bitwise/0040mark_binop_10 | 11 + .../shell/testcases/bitwise/0040mark_binop_11 | 11 + .../shell/testcases/bitwise/0040mark_binop_12 | 11 + .../shell/testcases/bitwise/0040mark_binop_13 | 11 + .../testcases/bitwise/0044payload_binop_0 | 11 + .../testcases/bitwise/0044payload_binop_1 | 11 + .../testcases/bitwise/0044payload_binop_2 | 11 + .../testcases/bitwise/0044payload_binop_3 | 11 + .../testcases/bitwise/0044payload_binop_4 | 11 + .../testcases/bitwise/0044payload_binop_5 | 11 + .../bitwise/dumps/0040mark_binop_10.nft | 6 + .../bitwise/dumps/0040mark_binop_11.nft | 6 + .../bitwise/dumps/0040mark_binop_12.nft | 6 + .../bitwise/dumps/0040mark_binop_13.nft | 6 + .../bitwise/dumps/0044payload_binop_0.nft | 6 + .../bitwise/dumps/0044payload_binop_1.nft | 6 + .../bitwise/dumps/0044payload_binop_2.nft | 6 + .../bitwise/dumps/0044payload_binop_3.nft | 6 + .../bitwise/dumps/0044payload_binop_4.nft | 6 + .../bitwise/dumps/0044payload_binop_5.nft | 6 + 47 files changed, 1062 insertions(+), 140 deletions(-) create mode 100755 tests/shell/testcases/bitwise/0040mark_binop_10 create mode 100755 tests/shell/testcases/bitwise/0040mark_binop_11 create mode 100755 tests/shell/testcases/bitwise/0040mark_binop_12 create mode 100755 tests/shell/testcases/bitwise/0040mark_binop_13 create mode 100755 tests/shell/testcases/bitwise/0044payload_binop_0 create mode 100755 tests/shell/testcases/bitwise/0044payload_binop_1 create mode 100755 tests/shell/testcases/bitwise/0044payload_binop_2 create mode 100755 tests/shell/testcases/bitwise/0044payload_binop_3 create mode 100755 tests/shell/testcases/bitwise/0044payload_binop_4 create mode 100755 tests/shell/testcases/bitwise/0044payload_binop_5 create mode 100644 tests/shell/testcases/bitwise/dumps/0040mark_binop_10.nft create mode 100644 tests/shell/testcases/bitwise/dumps/0040mark_binop_11.nft create mode 100644 tests/shell/testcases/bitwise/dumps/0040mark_binop_12.nft create mode 100644 tests/shell/testcases/bitwise/dumps/0040mark_binop_13.nft create mode 100644 tests/shell/testcases/bitwise/dumps/0044payload_binop_0.nft create mode 100644 tests/shell/testcases/bitwise/dumps/0044payload_binop_1.nft create mode 100644 tests/shell/testcases/bitwise/dumps/0044payload_binop_2.nft create mode 100644 tests/shell/testcases/bitwise/dumps/0044payload_binop_3.nft create mode 100644 tests/shell/testcases/bitwise/dumps/0044payload_binop_4.nft create mode 100644 tests/shell/testcases/bitwise/dumps/0044payload_binop_5.nft
Signature · 1 line
2.39.2
PATCH v5 1/8 netlink: support (de)linearization of new bitwise boolean operations
Hitherto, all boolean bitwise operationss have been converted to the form:
dst = (src & mask) ^ xor
and constant values have been required for xor and mask. This has meant that the right-hand operand of a boolean binop must be constant.
The kernel now supports performing AND, OR and XOR operations directly, on one register and an immediate value or on two registers, so we need to be able to generate and parse bitwise boolean expressions of this form.
If a boolean operation has a constant RHS, we continue to send a mask-and-xor expression to the kernel.
--- include/linux/netfilter/nf_tables.h | 19 +++++++-- src/netlink_delinearize.c | 64 ++++++++++++++++++++++------- src/netlink_linearize.c | 62 +++++++++++++++++++++++----- 3 files changed, 117 insertions(+), 28 deletions(-)
include/linux/netfilter/nf_tables.h +16 -3
@@ -555,16 +555,27 @@ enum nft_immediate_attributes {/** * enum nft_bitwise_ops - nf_tables bitwise operations * -* @NFT_BITWISE_BOOL: mask-and-xor operation used to implement NOT, AND, OR and -* XOR boolean operations +* @NFT_BITWISE_MASK_XOR: mask-and-xor operation used to implement NOT, AND, OR +* and XOR boolean operations * @NFT_BITWISE_LSHIFT: left-shift operation * @NFT_BITWISE_RSHIFT: right-shift operation +* @NFT_BITWISE_AND: and operation +* @NFT_BITWISE_OR: or operation +* @NFT_BITWISE_XOR: xor operation */ enum nft_bitwise_ops { -NFT_BITWISE_BOOL , +NFT_BITWISE_MASK_XOR ,NFT_BITWISE_LSHIFT ,NFT_BITWISE_RSHIFT , +NFT_BITWISE_AND , +NFT_BITWISE_OR , +NFT_BITWISE_XOR , }; +/* +* Old name for NFT_BITWISE_MASK_XOR, predating the addition of NFT_BITWISE_AND, +* NFT_BITWISE_OR and NFT_BITWISE_XOR. Retained for backwards-compatibility. +*/ +#define NFT_BITWISE_BOOL NFT_BITWISE_MASK_XOR/** * enum nft_bitwise_attributes - nf_tables bitwise expression netlink attributes
@@ -577,6 +588,7 @@ enum nft_bitwise_ops {* @NFTA_BITWISE_OP: type of operation (NLA_U32: nft_bitwise_ops) * @NFTA_BITWISE_DATA: argument for non-boolean operations * (NLA_NESTED: nft_data_attributes) +* @NFTA_BITWISE_SREG2: second source register (NLA_U32: nft_registers) * * The bitwise expression supports boolean and shift operations. It implements * the boolean operations by performing the following operation:
@@ -600,6 +612,7 @@ enum nft_bitwise_attributes {NFTA_BITWISE_XOR ,NFTA_BITWISE_OP ,NFTA_BITWISE_DATA , +NFTA_BITWISE_SREG2 , __NFTA_BITWISE_MAX };#define NFTA_BITWISE_MAX (__NFTA_BITWISE_MAX - 1)
src/netlink_delinearize.c +49 -15
94 lines not on this page · raw message
src/netlink_linearize.c +52 -10
100 lines not on this page · raw message
Signature · 1 line
2.39.2
PATCH v5 2/8 netlink_delinearize: refactor stmt_payload_binop_postprocess
In reply to Jeremy SowdenWe are about to add support for a new payload binop that needs to be post-processed, so move the contents of the two major cases (I and II) into separate functions to keep the function size reasonable.
Fix a typo in a comment while we're at it.
--- src/netlink_delinearize.c | 193 +++++++++++++++++++++----------------- 1 file changed, 109 insertions(+), 84 deletions(-)
src/netlink_delinearize.c +109 -84
@@ -3112,6 +3112,110 @@ static void stmt_payload_binop_pp(struct rule_pp_ctx *ctx, struct expr *binop) } } +static bool stmt_payload_binop_postprocess_i (struct rule_pp_ctx *ctx) +{ +struct expr *expr, *binop, *payload, *value, *mask; +struct stmt *stmt = ctx->stmt; +mpz_t tmp, bitmask; + + expr = stmt->payload.val; + +if (expr->op !=OP_OR ) +return false; + + value = expr->right; +if (value->etype !=EXPR_VALUE ) +return false; + + binop = expr->left; +if (binop->op !=OP_AND ) +return false; + + payload = binop->left; +if (payload->etype !=EXPR_PAYLOAD ) +return false; + +if (!payload_expr_cmp (stmt->payload.expr, payload)) +return false; + + mask = binop->right; +if (mask->etype !=EXPR_VALUE ) +return false; + +mpz_init (tmp); +mpz_set (tmp, mask->value); + +mpz_init_bitmask (bitmask, payload->len); +mpz_xor (bitmask, bitmask, mask->value); +mpz_xor (bitmask, bitmask, value->value); +mpz_set (mask->value, bitmask); +mpz_clear (bitmask); + +binop_postprocess (ctx, expr, &expr->left); +if (!payload_is_known (payload)) +mpz_set (mask->value, tmp); +else { +expr_free (stmt->payload.expr); + stmt->payload.expr =expr_get (payload); + stmt->payload.val =expr_get (expr->right); +expr_free (expr); + } + +mpz_clear (tmp); + +return true; +} + +static bool stmt_payload_binop_postprocess_ii (struct rule_pp_ctx *ctx) +{ +struct expr *expr, *payload, *value; +struct stmt *stmt = ctx->stmt; +mpz_t bitmask; + + expr = stmt->payload.val; + + value = expr->right; +if (value->etype !=EXPR_VALUE ) +return false; + +switch (expr->op) { +case OP_AND :/* IIa */ + payload = expr->left; +mpz_init_bitmask (bitmask, payload->len); +mpz_xor (bitmask, bitmask, value->value); +mpz_set (value->value, bitmask); +mpz_clear (bitmask); +break ; +case OP_OR :/* IIb */ +break ; +default :/* No idea */ +return false; + } + +stmt_payload_binop_pp (ctx, expr); +if (!payload_is_known (expr->left)) +return false; + +expr_free (stmt->payload.expr); + +switch (expr->op) { +case OP_AND : +/* Mask was used to match payload, i.e. +* user asked to set zero value. +*/ +mpz_set_ui (value->value,0 ); +break ; +default : +break ; + } + + stmt->payload.expr =expr_get (expr->left); + stmt->payload.val =expr_get (expr->right); +expr_free (expr); + +return true; +} +/** * stmt_payload_binop_postprocess - decode payload set binop *
@@ -3128,7 +3232,7 @@ static void stmt_payload_binop_pp(struct rule_pp_ctx *ctx, struct expr *binop)* a binop expression with a munged payload expression on the left * and a mask to clear the real payload offset/length. * -* So chech if we have one of the following binops: +* So check if we have one of the following binops: * I) * binop (|) * binop(&) value/set
@@ -3156,9 +3260,8 @@ static void stmt_payload_binop_pp(struct rule_pp_ctx *ctx, struct expr *binop)*/ static void stmt_payload_binop_postprocess (struct rule_pp_ctx *ctx) { -struct expr *expr, *binop, *payload, *value, *mask; +struct expr *expr;struct stmt *stmt = ctx->stmt; -mpz_t bitmask; expr = stmt->payload.val;
@@ -3166,93 +3269,15 @@ static void stmt_payload_binop_postprocess(struct rule_pp_ctx *ctx)return ;switch (expr->left->etype) { -case EXPR_BINOP : {/* I? */ -mpz_t tmp; - -if (expr->op !=OP_OR ) -return ; - - value = expr->right; -if (value->etype !=EXPR_VALUE ) -return ; - - binop = expr->left; -if (binop->op !=OP_AND ) -return ; - - payload = binop->left; -if (payload->etype !=EXPR_PAYLOAD ) -return ; - -if (!payload_expr_cmp (stmt->payload.expr, payload)) -return ; - - mask = binop->right; -if (mask->etype !=EXPR_VALUE ) -return ; - -mpz_init (tmp); -mpz_set (tmp, mask->value); - -mpz_init_bitmask (bitmask, payload->len); -mpz_xor (bitmask, bitmask, mask->value); -mpz_xor (bitmask, bitmask, value->value); -mpz_set (mask->value, bitmask); -mpz_clear (bitmask); - -binop_postprocess (ctx, expr, &expr->left); -if (!payload_is_known (payload)) { -mpz_set (mask->value, tmp); -mpz_clear (tmp); +case EXPR_BINOP :/* I? */ +if (stmt_payload_binop_postprocess_i (ctx))return ; - } -mpz_clear (tmp); -expr_free (stmt->payload.expr); - stmt->payload.expr =expr_get (payload); - stmt->payload.val =expr_get (expr->right); -expr_free (expr);break ; - }case EXPR_PAYLOAD :/* II? */ - value = expr->right; -if (value->etype !=EXPR_VALUE ) -return ; - -switch (expr->op) { -case OP_AND :/* IIa */ - payload = expr->left; -mpz_init_bitmask (bitmask, payload->len); -mpz_xor (bitmask, bitmask, value->value); -mpz_set (value->value, bitmask); -mpz_clear (bitmask); -break ; -case OP_OR :/* IIb */ -break ; -default :/* No idea */ -return ; - } - -stmt_payload_binop_pp (ctx, expr); -if (!payload_is_known (expr->left)) +if (stmt_payload_binop_postprocess_ii (ctx))return ; -expr_free (stmt->payload.expr); - -switch (expr->op) { -case OP_AND : -/* Mask was used to match payload, i.e. -* user asked to set zero value. -*/ -mpz_set_ui (value->value,0 ); -break ; -default : -break ; - } - - stmt->payload.expr =expr_get (expr->left); - stmt->payload.val =expr_get (expr->right); -expr_free (expr);break ;default :/* No idea */ break ;
Signature · 1 line
2.39.2
PATCH v5 3/8 netlink_delinearize: add support for processing variable payload statement arguments
In reply to Jeremy SowdenIf a user uses a variable payload expression in a payload statement, the structure of the statement value is not handled by the existing statement postprocessing function, so we need to extend it.
--- src/netlink_delinearize.c | 86 ++++++++++++++++++++++++++++++++++++--- 1 file changed, 80 insertions(+), 6 deletions(-)
src/netlink_delinearize.c +80 -6
@@ -3112,7 +3112,7 @@ static void stmt_payload_binop_pp(struct rule_pp_ctx *ctx, struct expr *binop) } } -static bool stmt_payload_binop_postprocess_i (struct rule_pp_ctx *ctx) +static bool stmt_payload_binop_postprocess_i_a (struct rule_pp_ctx *ctx) {struct expr *expr, *binop, *payload, *value, *mask;struct stmt *stmt = ctx->stmt;
@@ -3166,6 +3166,68 @@ static bool stmt_payload_binop_postprocess_i(struct rule_pp_ctx *ctx)return true; } +static bool stmt_payload_binop_postprocess_i_b (struct rule_pp_ctx *ctx) +{ +struct dl_proto_ctx *dl =dl_proto_ctx (ctx); +struct expr *expr, *payload, *mask, *xor; +struct stmt *stmt = ctx->stmt; +unsigned int shift; +mpz_t tmp, bitmask; + + expr = stmt->payload.val; + +if (expr->op !=OP_XOR ) +return false; + +if (expr->left->etype !=EXPR_BINOP ) +return false; + +if (expr->left->op !=OP_AND ) +return false; + +if (expr->right->etype ==EXPR_UNARY ) { +/* +* If the payload value was originally in a different byte-order +* from the payload expression, there will be a byte-order +* conversion to remove. +*/ + xor =expr_get (expr->right->arg); +expr_free (expr->right); + expr->right = xor; + }else + xor = expr->right; + + mask = expr->left->right; + payload = expr->left->left; + +mpz_init (tmp); +mpz_set (tmp, mask->value); + +mpz_init_bitmask (bitmask, payload->len); +mpz_xor (bitmask, bitmask, mask->value); +mpz_set (mask->value, bitmask); +mpz_clear (bitmask); + +if (payload_expr_trim (payload, mask, &dl->pctx, &shift)) +payload_match_postprocess (ctx, expr->left, payload); + +if (!payload_is_known (payload)) { +mpz_set (mask->value, tmp); + }else { +if (shift) { + expr->right =expr_get (xor->left); +expr_free (xor); + } +expr_free (stmt->payload.expr); + stmt->payload.expr =expr_get (payload); + stmt->payload.val =expr_get (expr->right); +expr_free (expr); + } + +mpz_clear (tmp); +return true; +} +static bool stmt_payload_binop_postprocess_ii (struct rule_pp_ctx *ctx) {struct expr *expr, *payload, *value;
@@ -3233,21 +3295,30 @@ static bool stmt_payload_binop_postprocess_ii(struct rule_pp_ctx *ctx)* and a mask to clear the real payload offset/length. * * So check if we have one of the following binops: -* I) +* +* Ia) * binop (|) * binop(&) value/set * payload value(mask) * -* This is the normal case, the | RHS is the value the user wants -* to set, the & RHS is the mask value that discards bits we need +* This is the normal constant case, the | RHS is the value the user +* wants to set, the & RHS is the mask value that discards bits we need * to clear but retains everything unrelated to the set operation. * +* Ib) +* binop (^) +* binop(&) value/set +* payload value(mask) +* +* The user wants to set a variable payload argument. The ^ RHS is the +* variable expression. The mask is as above. +* * IIa) * binop (&) * payload mask * * User specified a zero set value -- netlink bitwise decoding -* discarded the redundant "| 0" part. This is identical to I), +* discarded the redundant "| 0" part. This is identical to Ia), * we can just set value to 0 after we inferred the real payload size. * * IIb)
@@ -3270,7 +3341,10 @@ static void stmt_payload_binop_postprocess(struct rule_pp_ctx *ctx)switch (expr->left->etype) {case EXPR_BINOP :/* I? */ -if (stmt_payload_binop_postprocess_i (ctx)) +if (stmt_payload_binop_postprocess_i_a (ctx)) +return ; + +if (stmt_payload_binop_postprocess_i_b (ctx))return ;break ;
Signature · 1 line
2.39.2
PATCH v5 4/8 evaluate: prevent nested byte-order conversions
In reply to Jeremy SowdenThere is a an assertion in expr_evaluate_unary that checks that the operand of the unary operation is not itself a unary expression. Add a check to byteorder_conversion to ensure that this is the case by removing an existing unary operation, rather than adding a second one.
--- src/evaluate.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)
src/evaluate.c +6 -1
@@ -195,7 +195,12 @@ static int byteorder_conversion(struct eval_ctx *ctx, struct expr **expr,if (expr_is_constant (*expr) ||div_round_up ((*expr)->len,BITS_PER_BYTE ) <2 ) (*expr)->byteorder = byteorder; -else { +else if ((*expr)->etype ==EXPR_UNARY ) { +/* Remove existing conversion */ +struct expr *unary = *expr; + *expr =expr_get ((*expr)->arg); +expr_free (unary); + }else { op =byteorder_conversion_op (*expr, byteorder); *expr =unary_expr_alloc (&(*expr)->location, op, *expr);if (expr_evaluate (ctx, expr) <0 )
Signature · 1 line
2.39.2
PATCH v5 5/8 evaluate: preserve existing binop properties
In reply to Jeremy SowdenIn certain cases, such as evaluating payload statement arguments, we allocate new binop expressions and set properties such as length, data-type and byte-order. When the new expressions are themselves evaluated, these properties are overridden. Since the length of expression is set in all cases, check for this and preserve the length, data-type and byte-order for bitwise op's and the length for shifts.
Remove a couple of superfluous assignments for a left-shift which were being correctly overridden.
--- src/evaluate.c | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-)
src/evaluate.c +13 -4
@@ -1301,7 +1301,9 @@ static int expr_evaluate_shift(struct eval_ctx *ctx, struct expr **expr)unsigned int shift =mpz_get_uint32 (right->value);unsigned int max_shift_len; -if (ctx->stmt_len > left->len) +if (op->len) + max_shift_len = op->len; +else if (ctx->stmt_len > left->len) max_shift_len = ctx->stmt_len;else max_shift_len = left->len;
@@ -1335,7 +1337,16 @@ static int expr_evaluate_bitwise(struct eval_ctx *ctx, struct expr **expr)unsigned int max_len;int byteorder; -if (ctx->stmt_len > left->len) { +if (op->len) { + max_len = op->len; + byteorder = op->byteorder; + dtype = op->dtype; + +if (byteorder_conversion (ctx, &op->left, byteorder) <0 ) +return -1 ; + + left = op->left; + }else if (ctx->stmt_len > left->len) { max_len = ctx->stmt_len; byteorder =BYTEORDER_HOST_ENDIAN ; dtype = &integer_type;
@@ -2962,8 +2973,6 @@ static int stmt_evaluate_payload(struct eval_ctx *ctx, struct stmt *stmt) lshift =binop_expr_alloc (&payload->location,OP_LSHIFT , stmt->payload.val, off); - lshift->dtype = payload->dtype; - lshift->byteorder = payload->byteorder; stmt->payload.val = lshift; }
Signature · 1 line
2.39.2
PATCH v5 6/8 evaluate: allow binop expressions with variable right-hand operands
In reply to Jeremy SowdenHitherto, the kernel has required constant values for the xor and mask attributes of boolean bitwise expressions. This has meant that the right-hand operand of a boolean binop must be constant. Now the kernel has support for AND, OR and XOR operations with right-hand operands passed via registers, we can relax this restriction. Allow non-constant right-hand operands if the left-hand operand is not constant, e.g.:
ct mark & 0xffff0000 | meta mark & 0xffff
--- src/evaluate.c | 43 +++++++++++++++++++++++++++---------------- 1 file changed, 27 insertions(+), 16 deletions(-)
src/evaluate.c +27 -16
@@ -1369,16 +1369,18 @@ static int expr_evaluate_bitwise(struct eval_ctx *ctx, struct expr **expr) op->byteorder = byteorder; op->len = max_len; -if (expr_is_constant (left)) +if (expr_is_constant (left) &&expr_is_constant (op->right))return constant_binop_simplify (ctx, expr);return 0 ; }/* -* Binop expression: both sides must be of integer base type. The left -* hand side may be either constant or non-constant; in case its constant -* it must be a singleton. The ride hand side must always be a constant -* singleton. +* 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 +* singleton. For bitwise operations, the right-hand side must be constant if +* the left-hand side is constant; the right-hand side may be constant or +* non-constant, if the left-hand side is non-constant; for shifts, the +* right-hand side must be constant; if it is constant, it must be a singleton. */ static int expr_evaluate_binop (struct eval_ctx *ctx,struct expr **expr) {
@@ -1419,27 +1421,36 @@ static int expr_evaluate_binop(struct eval_ctx *ctx, struct expr **expr)"for %s expressions" , sym, expr_name(left)); -if (!expr_is_constant (right)) -return expr_binary_error (ctx->msgs, right, op, -"Right hand side of binary operation " -"(%s) must be constant" , sym); - -if (!expr_is_singleton (right)) -return expr_binary_error (ctx->msgs, left, op, -"Binary operation (%s) is undefined " -"for %s expressions" , - sym,expr_name (right)); -/* The grammar guarantees this */ assert (datatype_equal (expr_basetype (left),expr_basetype (right)));switch (op->op) {case OP_LSHIFT :case OP_RSHIFT : +if (!expr_is_constant (right)) +return expr_binary_error (ctx->msgs, right, op, +"Right hand side of binary operation " +"(%s) must be constant" , sym); + +if (!expr_is_singleton (right)) +return expr_binary_error (ctx->msgs, left, op, +"Binary operation (%s) is undefined " +"for %s expressions" , + sym,expr_name (right));return expr_evaluate_shift (ctx, expr);case OP_AND :case OP_XOR :case OP_OR : +if (expr_is_constant (left) && !expr_is_constant (right)) +return expr_binary_error (ctx->msgs, right, op, +"Right hand side of binary operation " +"(%s) must be constant" , sym); + +if (expr_is_constant (right) && !expr_is_singleton (right)) +return expr_binary_error (ctx->msgs, left, op, +"Binary operation (%s) is undefined " +"for %s expressions" , + sym,expr_name (right));return expr_evaluate_bitwise (ctx, expr);default :BUG ("invalid binary operation %u\n" , op->op);
Signature · 1 line
2.39.2
PATCH v5 7/8 parser_json: allow RHS mark and payload expressions
In reply to Jeremy SowdenSupport for binops with variable RHS's will make it possible to have ct and meta mark and payload expressions in the RHS. Relax the JSON parser accordingly.
Fix a typo in an adjacent comment.
--- src/parser_json.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
src/parser_json.c +4 -4
@@ -1454,20 +1454,20 @@ static struct expr *json_parse_expr(struct json_ctx *ctx, json_t *root) { "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 }, -/* below three are multiton_rhs_expr */ +/* below two 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 }, - { "payload", json_parse_payload_expr, CTX_F_STMT | CTX_F_PRIMARY | CTX_F_SET_RHS | CTX_F_MANGLE | CTX_F_SES | CTX_F_MAP | CTX_F_CONCAT }, + { "payload", json_parse_payload_expr, CTX_F_RHS | CTX_F_STMT | CTX_F_PRIMARY | CTX_F_SET_RHS | CTX_F_MANGLE | CTX_F_SES | CTX_F_MAP | CTX_F_CONCAT }, { "exthdr", json_parse_exthdr_expr, CTX_F_PRIMARY | CTX_F_SET_RHS | CTX_F_SES | CTX_F_MAP | CTX_F_CONCAT }, { "tcp option", json_parse_tcp_option_expr, CTX_F_PRIMARY | CTX_F_SET_RHS | CTX_F_MANGLE | CTX_F_SES | CTX_F_CONCAT }, { "ip option", json_parse_ip_option_expr, CTX_F_PRIMARY | CTX_F_SET_RHS | CTX_F_MANGLE | CTX_F_SES | CTX_F_CONCAT }, { "sctp chunk", json_parse_sctp_chunk_expr, CTX_F_PRIMARY | CTX_F_SET_RHS | CTX_F_MANGLE | CTX_F_SES | CTX_F_CONCAT }, - { "meta", json_parse_meta_expr, CTX_F_STMT | CTX_F_PRIMARY | CTX_F_SET_RHS | CTX_F_MANGLE | CTX_F_SES | CTX_F_MAP | CTX_F_CONCAT }, + { "meta", json_parse_meta_expr, CTX_F_RHS | CTX_F_STMT | CTX_F_PRIMARY | CTX_F_SET_RHS | CTX_F_MANGLE | CTX_F_SES | CTX_F_MAP | CTX_F_CONCAT }, { "osf", json_parse_osf_expr, CTX_F_STMT | CTX_F_PRIMARY | CTX_F_MAP | CTX_F_CONCAT }, { "ipsec", json_parse_xfrm_expr, CTX_F_PRIMARY | CTX_F_MAP | CTX_F_CONCAT }, { "socket", json_parse_socket_expr, CTX_F_PRIMARY | CTX_F_CONCAT }, { "rt", json_parse_rt_expr, CTX_F_STMT | CTX_F_PRIMARY | CTX_F_SET_RHS | CTX_F_SES | CTX_F_MAP | CTX_F_CONCAT }, - { "ct", json_parse_ct_expr, CTX_F_STMT | CTX_F_PRIMARY | CTX_F_SET_RHS | CTX_F_MANGLE | CTX_F_SES | CTX_F_MAP | CTX_F_CONCAT }, + { "ct", json_parse_ct_expr, CTX_F_RHS | CTX_F_STMT | CTX_F_PRIMARY | CTX_F_SET_RHS | CTX_F_MANGLE | CTX_F_SES | CTX_F_MAP | CTX_F_CONCAT }, { "numgen", json_parse_numgen_expr, CTX_F_STMT | CTX_F_PRIMARY | CTX_F_SET_RHS | CTX_F_SES | CTX_F_MAP | CTX_F_CONCAT },/* below two are hash expr */ { "jhash", json_parse_hash_expr, CTX_F_STMT | CTX_F_PRIMARY | CTX_F_SET_RHS | CTX_F_SES | CTX_F_MAP | CTX_F_CONCAT },
Signature · 1 line
2.39.2
PATCH v5 8/8 tests: add tests for binops with variable RHS operands
In reply to Jeremy SowdenAdd tests to validate setting marks and payloads with statement arguments that include binops with variable RHS operands.
--- tests/py/any/ct.t | 1 + tests/py/any/ct.t.json | 37 +++++++++ tests/py/any/ct.t.payload | 9 +++ tests/py/inet/meta.t | 2 + tests/py/inet/meta.t.json | 37 +++++++++ tests/py/inet/meta.t.payload | 9 +++ tests/py/ip/ct.t | 1 + tests/py/ip/ct.t.json | 36 +++++++++ tests/py/ip/ct.t.payload | 11 +++ tests/py/ip/ip.t | 2 + tests/py/ip/ip.t.json | 77 ++++++++++++++++++- tests/py/ip/ip.t.payload | 28 +++++++ tests/py/ip/ip.t.payload.bridge | 32 ++++++++ tests/py/ip/ip.t.payload.inet | 32 ++++++++ tests/py/ip/ip.t.payload.netdev | 32 ++++++++ tests/py/ip6/ct.t | 1 + tests/py/ip6/ct.t.json | 36 +++++++++ tests/py/ip6/ct.t.payload | 12 +++ tests/py/ip6/ip6.t | 2 + tests/py/ip6/ip6.t.json | 76 ++++++++++++++++++ tests/py/ip6/ip6.t.payload.inet | 36 +++++++++ tests/py/ip6/ip6.t.payload.ip6 | 32 ++++++++ .../shell/testcases/bitwise/0040mark_binop_10 | 11 +++ .../shell/testcases/bitwise/0040mark_binop_11 | 11 +++ .../shell/testcases/bitwise/0040mark_binop_12 | 11 +++ .../shell/testcases/bitwise/0040mark_binop_13 | 11 +++ .../testcases/bitwise/0044payload_binop_0 | 11 +++ .../testcases/bitwise/0044payload_binop_1 | 11 +++ .../testcases/bitwise/0044payload_binop_2 | 11 +++ .../testcases/bitwise/0044payload_binop_3 | 11 +++ .../testcases/bitwise/0044payload_binop_4 | 11 +++ .../testcases/bitwise/0044payload_binop_5 | 11 +++ .../bitwise/dumps/0040mark_binop_10.nft | 6 ++ .../bitwise/dumps/0040mark_binop_11.nft | 6 ++ .../bitwise/dumps/0040mark_binop_12.nft | 6 ++ .../bitwise/dumps/0040mark_binop_13.nft | 6 ++ .../bitwise/dumps/0044payload_binop_0.nft | 6 ++ .../bitwise/dumps/0044payload_binop_1.nft | 6 ++ .../bitwise/dumps/0044payload_binop_2.nft | 6 ++ .../bitwise/dumps/0044payload_binop_3.nft | 6 ++ .../bitwise/dumps/0044payload_binop_4.nft | 6 ++ .../bitwise/dumps/0044payload_binop_5.nft | 6 ++ 42 files changed, 710 insertions(+), 1 deletion(-) create mode 100755 tests/shell/testcases/bitwise/0040mark_binop_10 create mode 100755 tests/shell/testcases/bitwise/0040mark_binop_11 create mode 100755 tests/shell/testcases/bitwise/0040mark_binop_12 create mode 100755 tests/shell/testcases/bitwise/0040mark_binop_13 create mode 100755 tests/shell/testcases/bitwise/0044payload_binop_0 create mode 100755 tests/shell/testcases/bitwise/0044payload_binop_1 create mode 100755 tests/shell/testcases/bitwise/0044payload_binop_2 create mode 100755 tests/shell/testcases/bitwise/0044payload_binop_3 create mode 100755 tests/shell/testcases/bitwise/0044payload_binop_4 create mode 100755 tests/shell/testcases/bitwise/0044payload_binop_5 create mode 100644 tests/shell/testcases/bitwise/dumps/0040mark_binop_10.nft create mode 100644 tests/shell/testcases/bitwise/dumps/0040mark_binop_11.nft create mode 100644 tests/shell/testcases/bitwise/dumps/0040mark_binop_12.nft create mode 100644 tests/shell/testcases/bitwise/dumps/0040mark_binop_13.nft create mode 100644 tests/shell/testcases/bitwise/dumps/0044payload_binop_0.nft create mode 100644 tests/shell/testcases/bitwise/dumps/0044payload_binop_1.nft create mode 100644 tests/shell/testcases/bitwise/dumps/0044payload_binop_2.nft create mode 100644 tests/shell/testcases/bitwise/dumps/0044payload_binop_3.nft create mode 100644 tests/shell/testcases/bitwise/dumps/0044payload_binop_4.nft create mode 100644 tests/shell/testcases/bitwise/dumps/0044payload_binop_5.nft
tests/py/any/ct.t +1 -0
@@ -61,6 +61,7 @@ ct mark set 0x11;ok;ct mark set 0x00000011 ct mark set mark;ok;ct mark set meta mark ct mark set (meta mark | 0x10) << 8;ok;ct mark set (meta mark | 0x00000010) << 8 ct mark set mark map { 1 : 10, 2 : 20, 3 : 30 };ok;ct mark set meta mark map { 0x00000003 : 0x0000001e, 0x00000002 : 0x00000014, 0x00000001 : 0x0000000a} +ct mark set ct mark and 0xffff0000 or meta mark and 0xffff;ok;ct mark set ct mark & 0xffff0000 | meta mark & 0x0000ffff ct mark set {0x11333, 0x11};fail ct zone set {123, 127};fail
tests/py/any/ct.t.json +37 -0
@@ -817,6 +817,43 @@ } ] +# ct mark set ct mark and 0xffff0000 or meta mark and 0xffff +[ + { + "mangle": { + "key": { + "ct": { + "key": "mark" + } + }, + "value": { + "|": [ + { + "&": [ + { + "ct": { + "key": "mark" + } + }, + 4294901760 + ] + }, + { + "&": [ + { + "meta": { + "key": "mark" + } + }, + 65535 + ] + } + ] + } + } + } +] + # ct expiration 30s [ {
tests/py/any/ct.t.payload +9 -0
@@ -336,6 +336,15 @@ ip test-ip4 output [ lookup reg 1 set __map%d dreg 1 ] [ ct set mark with reg 1 ] +# ct mark set ct mark and 0xffff0000 or meta mark and 0xffff +ip + [ ct load mark => reg 1 ] + [ bitwise reg 1 = ( reg 1 & 0xffff0000 ) ^ 0x00000000 ] + [ meta load mark => reg 2 ] + [ bitwise reg 2 = ( reg 2 & 0x0000ffff ) ^ 0x00000000 ] + [ bitwise reg 1 = ( reg 1 | reg 2 ) ] + [ ct set mark with reg 1 ] + # ct original bytes > 100000 ip test-ip4 output [ ct load bytes => reg 1 , dir original ]
tests/py/inet/meta.t +2 -0
@@ -25,3 +25,5 @@ meta mark set ct mark >> 8;ok meta mark . tcp dport { 0x0000000a-0x00000014 . 80-90, 0x00100000-0x00100123 . 100-120 };ok ip saddr . meta mark { 1.2.3.4 . 0x00000100 , 1.2.3.6-1.2.3.8 . 0x00000200-0x00000300 };ok ip saddr . meta mark { 1.2.3.4 . 0x00000100 , 5.6.7.8 . 0x00000200 };ok + +meta mark set ct mark and 0xffff0000 or meta mark and 0xffff;ok;meta mark set ct mark & 0xffff0000 | meta mark & 0x0000ffff
tests/py/inet/meta.t.json +37 -0
@@ -236,6 +236,43 @@ } ] +# meta mark set ct mark and 0xffff0000 or meta mark and 0xffff +[ + { + "mangle": { + "key": { + "meta": { + "key": "mark" + } + }, + "value": { + "|": [ + { + "&": [ + { + "ct": { + "key": "mark" + } + }, + 4294901760 + ] + }, + { + "&": [ + { + "meta": { + "key": "mark" + } + }, + 65535 + ] + } + ] + } + } + } +] + # meta protocol ip udp dport 67 [ {
tests/py/inet/meta.t.payload +9 -0
@@ -80,6 +80,15 @@ inet test-inet input [ bitwise reg 1 = ( reg 1 >> 0x00000008 ) ] [ meta set mark with reg 1 ] +# meta mark set ct mark and 0xffff0000 or meta mark and 0xffff +inet test-inet input + [ ct load mark => reg 1 ] + [ bitwise reg 1 = ( reg 1 & 0xffff0000 ) ^ 0x00000000 ] + [ meta load mark => reg 2 ] + [ bitwise reg 2 = ( reg 2 & 0x0000ffff ) ^ 0x00000000 ] + [ bitwise reg 1 = ( reg 1 | reg 2 ) ] + [ meta set mark with reg 1 ] + # meta protocol ip udp dport 67 inet test-inet input [ meta load protocol => reg 1 ]
tests/py/ip/ct.t +1 -0
@@ -34,3 +34,4 @@ ct mark set ip dscp & 0x0f << 1;ok;ct mark set ip dscp & af33 ct mark set ip dscp & 0x0f << 2;ok;ct mark set ip dscp & 0x3c ct mark set ip dscp | 0x04;ok ct mark set ip dscp | 1 << 20;ok;ct mark set ip dscp | 0x100000 +ct mark set ct mark | ip dscp | 0x200 counter;ok;ct mark set ct mark | ip dscp | 0x00000200 counter
tests/py/ip/ct.t.json +36 -0
@@ -479,3 +479,39 @@ } } ] + +# ct mark set ct mark | ip dscp | 0x200 counter +[ + { + "mangle": { + "key": { + "ct": { + "key": "mark" + } + }, + "value": { + "|": [ + { + "|": [ + { + "ct": { + "key": "mark" + } + }, + { + "payload": { + "field": "dscp", + "protocol": "ip" + } + } + ] + }, + 512 + ] + } + } + }, + { + "counter": null + } +]
tests/py/ip/ct.t.payload +11 -0
@@ -134,3 +134,14 @@ ip test-ip4 output [ bitwise reg 1 = ( reg 1 >> 0x00000002 ) ] [ bitwise reg 1 = ( reg 1 & 0xffefffff ) ^ 0x00100000 ] [ ct set mark with reg 1 ] + +# ct mark set ct mark | ip dscp | 0x200 counter +ip test-ip4 output + [ ct load mark => reg 1 ] + [ payload load 1b @ network header + 1 => reg 2 ] + [ bitwise reg 2 = ( reg 2 & 0x000000fc ) ^ 0x00000000 ] + [ bitwise reg 2 = ( reg 2 >> 0x00000002 ) ] + [ bitwise reg 1 = ( reg 1 | reg 2 ) ] + [ bitwise reg 1 = ( reg 1 & 0xfffffdff ) ^ 0x00000200 ] + [ ct set mark with reg 1 ] + [ counter pkts 0 bytes 0 ]
tests/py/ip/ip.t +2 -0
@@ -124,6 +124,8 @@ iif "lo" ip protocol set 1;ok iif "lo" ip dscp set af23;ok iif "lo" ip dscp set cs0;ok +iif "lo" ip dscp set (meta mark & 0xfc000000) >> 26;ok +iif "lo" ip dscp set ip dscp & 0xc;ok;iif "lo" ip dscp set ip dscp & af12 ip saddr . ip daddr { 192.0.2.1 . 10.0.0.1-10.0.0.2 };ok ip saddr . ip daddr vmap { 192.168.5.1-192.168.5.128 . 192.168.6.1-192.168.6.128 : accept };ok
tests/py/ip/ip.t.json +76 -1
86 lines not on this page · raw message
tests/py/ip/ip.t.payload +28 -0
@@ -490,6 +490,34 @@ ip test-ip4 input [ bitwise reg 1 = ( reg 1 & 0x000003ff ) ^ 0x00000000 ] [ payload write reg 1 => 2b @ network header + 0 csum_type 1 csum_off 10 csum_flags 0x0 ] +# iif "lo" ip dscp set (meta mark & 0xfc000000) >> 26 +ip test-ip4 input + [ meta load iif => reg 1 ] + [ cmp eq reg 1 0x00000001 ] + [ payload load 2b @ network header + 0 => reg 1 ] + [ bitwise reg 1 = ( reg 1 & 0x000003ff ) ^ 0x00000000 ] + [ meta load mark => reg 2 ] + [ bitwise reg 2 = ( reg 2 & 0xfc000000 ) ^ 0x00000000 ] + [ bitwise reg 2 = ( reg 2 >> 0x0000001a ) ] + [ bitwise reg 2 = ( reg 2 << 0x00000002 ) ] + [ byteorder reg 2 = hton(reg 2, 4, 4) ] + [ bitwise reg 1 = ( reg 1 ^ reg 2 ) ] + [ payload write reg 1 => 2b @ network header + 0 csum_type 1 csum_off 10 csum_flags 0x0 ] + +# iif "lo" ip dscp set ip dscp & 0xc +ip test-ip4 input + [ meta load iif => reg 1 ] + [ cmp eq reg 1 0x00000001 ] + [ payload load 2b @ network header + 0 => reg 1 ] + [ bitwise reg 1 = ( reg 1 & 0x000003ff ) ^ 0x00000000 ] + [ payload load 1b @ network header + 1 => reg 2 ] + [ bitwise reg 2 = ( reg 2 & 0x000000fc ) ^ 0x00000000 ] + [ bitwise reg 2 = ( reg 2 >> 0x00000002 ) ] + [ bitwise reg 2 = ( reg 2 & 0x0000000c ) ^ 0x00000000 ] + [ bitwise reg 2 = ( reg 2 << 0x00000002 ) ] + [ bitwise reg 1 = ( reg 1 ^ reg 2 ) ] + [ payload write reg 1 => 2b @ network header + 0 csum_type 1 csum_off 10 csum_flags 0x0 ] + # iif "lo" ip ttl set 23 ip test-ip4 input [ meta load iif => reg 1 ]
tests/py/ip/ip.t.payload.bridge +32 -0
@@ -662,6 +662,38 @@ bridge test-bridge input [ bitwise reg 1 = ( reg 1 & 0x000003ff ) ^ 0x00000000 ] [ payload write reg 1 => 2b @ network header + 0 csum_type 1 csum_off 10 csum_flags 0x0 ] +# iif "lo" ip dscp set (meta mark & 0xfc000000) >> 26 +bridge test-bridge input + [ meta load iif => reg 1 ] + [ cmp eq reg 1 0x00000001 ] + [ meta load protocol => reg 1 ] + [ cmp eq reg 1 0x00000008 ] + [ payload load 2b @ network header + 0 => reg 1 ] + [ bitwise reg 1 = ( reg 1 & 0x000003ff ) ^ 0x00000000 ] + [ meta load mark => reg 2 ] + [ bitwise reg 2 = ( reg 2 & 0xfc000000 ) ^ 0x00000000 ] + [ bitwise reg 2 = ( reg 2 >> 0x0000001a ) ] + [ bitwise reg 2 = ( reg 2 << 0x00000002 ) ] + [ byteorder reg 2 = hton(reg 2, 4, 4) ] + [ bitwise reg 1 = ( reg 1 ^ reg 2 ) ] + [ payload write reg 1 => 2b @ network header + 0 csum_type 1 csum_off 10 csum_flags 0x0 ] + +# iif "lo" ip dscp set ip dscp & 0xc +bridge test-bridge input + [ meta load iif => reg 1 ] + [ cmp eq reg 1 0x00000001 ] + [ meta load protocol => reg 1 ] + [ cmp eq reg 1 0x00000008 ] + [ payload load 2b @ network header + 0 => reg 1 ] + [ bitwise reg 1 = ( reg 1 & 0x000003ff ) ^ 0x00000000 ] + [ payload load 1b @ network header + 1 => reg 2 ] + [ bitwise reg 2 = ( reg 2 & 0x000000fc ) ^ 0x00000000 ] + [ bitwise reg 2 = ( reg 2 >> 0x00000002 ) ] + [ bitwise reg 2 = ( reg 2 & 0x0000000c ) ^ 0x00000000 ] + [ bitwise reg 2 = ( reg 2 << 0x00000002 ) ] + [ bitwise reg 1 = ( reg 1 ^ reg 2 ) ] + [ payload write reg 1 => 2b @ network header + 0 csum_type 1 csum_off 10 csum_flags 0x0 ] + # ip saddr . ip daddr { 192.0.2.1 . 10.0.0.1-10.0.0.2 } __set%d test-bridge 87 size 1 __set%d test-bridge 0
tests/py/ip/ip.t.payload.inet +32 -0
@@ -642,6 +642,38 @@ inet test-inet input [ bitwise reg 1 = ( reg 1 & 0x000003ff ) ^ 0x00000000 ] [ payload write reg 1 => 2b @ network header + 0 csum_type 1 csum_off 10 csum_flags 0x0 ] +# iif "lo" ip dscp set (meta mark & 0xfc000000) >> 26 +inet test-inet input + [ meta load iif => reg 1 ] + [ cmp eq reg 1 0x00000001 ] + [ meta load nfproto => reg 1 ] + [ cmp eq reg 1 0x00000002 ] + [ payload load 2b @ network header + 0 => reg 1 ] + [ bitwise reg 1 = ( reg 1 & 0x000003ff ) ^ 0x00000000 ] + [ meta load mark => reg 2 ] + [ bitwise reg 2 = ( reg 2 & 0xfc000000 ) ^ 0x00000000 ] + [ bitwise reg 2 = ( reg 2 >> 0x0000001a ) ] + [ bitwise reg 2 = ( reg 2 << 0x00000002 ) ] + [ byteorder reg 2 = hton(reg 2, 4, 4) ] + [ bitwise reg 1 = ( reg 1 ^ reg 2 ) ] + [ payload write reg 1 => 2b @ network header + 0 csum_type 1 csum_off 10 csum_flags 0x0 ] + +# iif "lo" ip dscp set ip dscp & 0xc +inet test-inet input + [ meta load iif => reg 1 ] + [ cmp eq reg 1 0x00000001 ] + [ meta load nfproto => reg 1 ] + [ cmp eq reg 1 0x00000002 ] + [ payload load 2b @ network header + 0 => reg 1 ] + [ bitwise reg 1 = ( reg 1 & 0x000003ff ) ^ 0x00000000 ] + [ payload load 1b @ network header + 1 => reg 2 ] + [ bitwise reg 2 = ( reg 2 & 0x000000fc ) ^ 0x00000000 ] + [ bitwise reg 2 = ( reg 2 >> 0x00000002 ) ] + [ bitwise reg 2 = ( reg 2 & 0x0000000c ) ^ 0x00000000 ] + [ bitwise reg 2 = ( reg 2 << 0x00000002 ) ] + [ bitwise reg 1 = ( reg 1 ^ reg 2 ) ] + [ payload write reg 1 => 2b @ network header + 0 csum_type 1 csum_off 10 csum_flags 0x0 ] + # iif "lo" ip ttl set 23 inet test-inet input [ meta load iif => reg 1 ]
tests/py/ip/ip.t.payload.netdev +32 -0
@@ -642,6 +642,38 @@ netdev test-netdev ingress [ bitwise reg 1 = ( reg 1 & 0x000003ff ) ^ 0x00000000 ] [ payload write reg 1 => 2b @ network header + 0 csum_type 1 csum_off 10 csum_flags 0x0 ] +# iif "lo" ip dscp set (meta mark & 0xfc000000) >> 26 +netdev test-netdev ingress + [ meta load iif => reg 1 ] + [ cmp eq reg 1 0x00000001 ] + [ meta load protocol => reg 1 ] + [ cmp eq reg 1 0x00000008 ] + [ payload load 2b @ network header + 0 => reg 1 ] + [ bitwise reg 1 = ( reg 1 & 0x000003ff ) ^ 0x00000000 ] + [ meta load mark => reg 2 ] + [ bitwise reg 2 = ( reg 2 & 0xfc000000 ) ^ 0x00000000 ] + [ bitwise reg 2 = ( reg 2 >> 0x0000001a ) ] + [ bitwise reg 2 = ( reg 2 << 0x00000002 ) ] + [ byteorder reg 2 = hton(reg 2, 4, 4) ] + [ bitwise reg 1 = ( reg 1 ^ reg 2 ) ] + [ payload write reg 1 => 2b @ network header + 0 csum_type 1 csum_off 10 csum_flags 0x0 ] + +# iif "lo" ip dscp set ip dscp & 0xc +netdev test-netdev ingress + [ meta load iif => reg 1 ] + [ cmp eq reg 1 0x00000001 ] + [ meta load protocol => reg 1 ] + [ cmp eq reg 1 0x00000008 ] + [ payload load 2b @ network header + 0 => reg 1 ] + [ bitwise reg 1 = ( reg 1 & 0x000003ff ) ^ 0x00000000 ] + [ payload load 1b @ network header + 1 => reg 2 ] + [ bitwise reg 2 = ( reg 2 & 0x000000fc ) ^ 0x00000000 ] + [ bitwise reg 2 = ( reg 2 >> 0x00000002 ) ] + [ bitwise reg 2 = ( reg 2 & 0x0000000c ) ^ 0x00000000 ] + [ bitwise reg 2 = ( reg 2 << 0x00000002 ) ] + [ bitwise reg 1 = ( reg 1 ^ reg 2 ) ] + [ payload write reg 1 => 2b @ network header + 0 csum_type 1 csum_off 10 csum_flags 0x0 ] + # iif "lo" ip ttl set 23 netdev test-netdev ingress [ meta load iif => reg 1 ]
tests/py/ip6/ct.t +1 -0
@@ -7,3 +7,4 @@ ct mark set ip6 dscp << 26 | 0x10;ok ct mark set ip6 dscp | 0x04;ok ct mark set ip6 dscp | 0xff000000;ok ct mark set ip6 dscp & 0x0f << 2;ok;ct mark set ip6 dscp & 0x3c +ct mark set ct mark | ip6 dscp | 0x200 counter;ok;ct mark set ct mark | ip6 dscp | 0x00000200 counter
tests/py/ip6/ct.t.json +36 -0
@@ -291,3 +291,39 @@ } } ] + +# ct mark set ct mark | ip6 dscp | 0x200 counter +[ + { + "mangle": { + "key": { + "ct": { + "key": "mark" + } + }, + "value": { + "|": [ + { + "|": [ + { + "ct": { + "key": "mark" + } + }, + { + "payload": { + "field": "dscp", + "protocol": "ip6" + } + } + ] + }, + 512 + ] + } + } + }, + { + "counter": null + } +]
tests/py/ip6/ct.t.payload +12 -0
@@ -44,3 +44,15 @@ ip6 test-ip6 output [ byteorder reg 1 = ntoh(reg 1, 2, 1) ] [ bitwise reg 1 = ( reg 1 & 0x0000003c ) ^ 0x00000000 ] [ ct set mark with reg 1 ] + +# ct mark set ct mark | ip6 dscp | 0x200 counter +ip6 test-ip6 output + [ ct load mark => reg 1 ] + [ payload load 2b @ network header + 0 => reg 2 ] + [ bitwise reg 2 = ( reg 2 & 0x0000c00f ) ^ 0x00000000 ] + [ bitwise reg 2 = ( reg 2 >> 0x00000006 ) ] + [ byteorder reg 2 = ntoh(reg 2, 2, 1) ] + [ bitwise reg 1 = ( reg 1 | reg 2 ) ] + [ bitwise reg 1 = ( reg 1 & 0xfffffdff ) ^ 0x00000200 ] + [ ct set mark with reg 1 ] + [ counter pkts 0 bytes 0 ]
tests/py/ip6/ip6.t +2 -0
@@ -141,6 +141,8 @@ iif "lo" ip6 daddr set ::1;ok iif "lo" ip6 hoplimit set 1;ok iif "lo" ip6 dscp set af42;ok iif "lo" ip6 dscp set 63;ok;iif "lo" ip6 dscp set 0x3f +iif "lo" ip6 dscp set (ct mark & 0xfc000000) >> 26;ok +iif "lo" ip6 dscp set ip6 dscp & 0xc;ok;iif "lo" ip6 dscp set ip6 dscp & 0x0c iif "lo" ip6 ecn set ect0;ok iif "lo" ip6 ecn set ce;ok
tests/py/ip6/ip6.t.json +76 -0
82 lines not on this page · raw message
tests/py/ip6/ip6.t.payload.inet +36 -0
@@ -589,6 +589,42 @@ inet test-inet input [ bitwise reg 1 = ( reg 1 & 0x00003ff0 ) ^ 0x0000c00f ] [ payload write reg 1 => 2b @ network header + 0 csum_type 0 csum_off 0 csum_flags 0x0 ] +# iif "lo" ip6 dscp set (ct mark & 0xfc000000) >> 26 +inet test-inet input + [ meta load iif => reg 1 ] + [ cmp eq reg 1 0x00000001 ] + [ meta load nfproto => reg 1 ] + [ cmp eq reg 1 0x0000000a ] + [ payload load 2b @ network header + 0 => reg 1 ] + [ bitwise reg 1 = ( reg 1 & 0x00003ff0 ) ^ 0x00000000 ] + [ ct load mark => reg 2 ] + [ bitwise reg 2 = ( reg 2 & 0xfc000000 ) ^ 0x00000000 ] + [ bitwise reg 2 = ( reg 2 >> 0x0000001a ) ] + [ bitwise reg 2 = ( reg 2 << 0x00000006 ) ] + [ byteorder reg 2 = hton(reg 2, 4, 4) ] + [ bitwise reg 1 = ( reg 1 ^ reg 2 ) ] + [ payload write reg 1 => 2b @ network header + 0 csum_type 0 csum_off 0 csum_flags 0x0 ] + +# iif "lo" ip6 dscp set ip6 dscp & 0xc +inet test-inet input + [ meta load iif => reg 1 ] + [ cmp eq reg 1 0x00000001 ] + [ meta load nfproto => reg 1 ] + [ cmp eq reg 1 0x0000000a ] + [ payload load 2b @ network header + 0 => reg 1 ] + [ bitwise reg 1 = ( reg 1 & 0x00003ff0 ) ^ 0x00000000 ] + [ payload load 2b @ network header + 0 => reg 2 ] + [ bitwise reg 2 = ( reg 2 & 0x0000c00f ) ^ 0x00000000 ] + [ byteorder reg 2 = ntoh(reg 2, 2, 1) ] + [ bitwise reg 2 = ( reg 2 >> 0x00000006 ) ] + [ byteorder reg 2 = hton(reg 2, 2, 1) ] + [ bitwise reg 2 = ( reg 2 & 0x00000c00 ) ^ 0x00000000 ] + [ byteorder reg 2 = ntoh(reg 2, 2, 1) ] + [ bitwise reg 2 = ( reg 2 << 0x00000006 ) ] + [ byteorder reg 2 = hton(reg 2, 2, 1) ] + [ bitwise reg 1 = ( reg 1 ^ reg 2 ) ] + [ payload write reg 1 => 2b @ network header + 0 csum_type 0 csum_off 0 csum_flags 0x0 ] + # iif "lo" ip6 ecn set ect0 inet test-inet input [ meta load iif => reg 1 ]
tests/py/ip6/ip6.t.payload.ip6 +32 -0
@@ -439,6 +439,38 @@ ip6 test-ip6 input [ bitwise reg 1 = ( reg 1 & 0x00003ff0 ) ^ 0x0000c00f ] [ payload write reg 1 => 2b @ network header + 0 csum_type 0 csum_off 0 csum_flags 0x0 ] +# iif "lo" ip6 dscp set (ct mark & 0xfc000000) >> 26 +ip6 test-ip6 input + [ meta load iif => reg 1 ] + [ cmp eq reg 1 0x00000001 ] + [ payload load 2b @ network header + 0 => reg 1 ] + [ bitwise reg 1 = ( reg 1 & 0x00003ff0 ) ^ 0x00000000 ] + [ ct load mark => reg 2 ] + [ bitwise reg 2 = ( reg 2 & 0xfc000000 ) ^ 0x00000000 ] + [ bitwise reg 2 = ( reg 2 >> 0x0000001a ) ] + [ bitwise reg 2 = ( reg 2 << 0x00000006 ) ] + [ byteorder reg 2 = hton(reg 2, 4, 4) ] + [ bitwise reg 1 = ( reg 1 ^ reg 2 ) ] + [ payload write reg 1 => 2b @ network header + 0 csum_type 0 csum_off 0 csum_flags 0x0 ] + +# iif "lo" ip6 dscp set ip6 dscp & 0xc +ip6 test-ip6 input + [ meta load iif => reg 1 ] + [ cmp eq reg 1 0x00000001 ] + [ payload load 2b @ network header + 0 => reg 1 ] + [ bitwise reg 1 = ( reg 1 & 0x00003ff0 ) ^ 0x00000000 ] + [ payload load 2b @ network header + 0 => reg 2 ] + [ bitwise reg 2 = ( reg 2 & 0x0000c00f ) ^ 0x00000000 ] + [ byteorder reg 2 = ntoh(reg 2, 2, 1) ] + [ bitwise reg 2 = ( reg 2 >> 0x00000006 ) ] + [ byteorder reg 2 = hton(reg 2, 2, 1) ] + [ bitwise reg 2 = ( reg 2 & 0x00000c00 ) ^ 0x00000000 ] + [ byteorder reg 2 = ntoh(reg 2, 2, 1) ] + [ bitwise reg 2 = ( reg 2 << 0x00000006 ) ] + [ byteorder reg 2 = hton(reg 2, 2, 1) ] + [ bitwise reg 1 = ( reg 1 ^ reg 2 ) ] + [ payload write reg 1 => 2b @ network header + 0 csum_type 0 csum_off 0 csum_flags 0x0 ] + # iif "lo" ip6 ecn set ect0 ip6 test-ip6 input [ meta load iif => reg 1 ]
tests/shell/testcases/bitwise/0040mark_binop_10 new file +11 -0
@@ -0,0 +1,11 @@ +#!/bin/bash + +set -e + +RULESET=" + add table t + add chain t c { type filter hook output priority filter; } + add rule t c ct mark set ct mark and 0xffff0000 or meta mark and 0xffff +" + +$NFT -f - <<< "$RULESET"
tests/shell/testcases/bitwise/0040mark_binop_11 new file +11 -0
@@ -0,0 +1,11 @@ +#!/bin/bash + +set -e + +RULESET=" + add table t + add chain t c { type filter hook input priority filter; } + add rule t c meta mark set ct mark and 0xffff0000 or meta mark and 0xffff +" + +$NFT -f - <<< "$RULESET"
tests/shell/testcases/bitwise/0040mark_binop_12 new file +11 -0
@@ -0,0 +1,11 @@ +#!/bin/bash + +set -e + +RULESET=" + add table ip6 t + add chain ip6 t c { type filter hook output priority filter; } + add rule ip6 t c ct mark set ct mark and 0xffff0000 or meta mark and 0xffff +" + +$NFT -f - <<< "$RULESET"
tests/shell/testcases/bitwise/0040mark_binop_13 new file +11 -0
@@ -0,0 +1,11 @@ +#!/bin/bash + +set -e + +RULESET=" + add table ip6 t + add chain ip6 t c { type filter hook input priority filter; } + add rule ip6 t c meta mark set ct mark and 0xffff0000 or meta mark and 0xffff +" + +$NFT -f - <<< "$RULESET"
tests/shell/testcases/bitwise/0044payload_binop_0 new file +11 -0
@@ -0,0 +1,11 @@ +#!/bin/bash + +set -e + +RULESET=" + add table t + add chain t c { type filter hook output priority filter; } + add rule t c ip dscp set (ct mark & 0xfc000000) >> 26 +" + +$NFT -f - <<< "$RULESET"
tests/shell/testcases/bitwise/0044payload_binop_1 new file +11 -0
@@ -0,0 +1,11 @@ +#!/bin/bash + +set -e + +RULESET=" + add table t + add chain t c { type filter hook output priority filter; } + add rule t c ip dscp set ip dscp and 0xc +" + +$NFT -f - <<< "$RULESET"
tests/shell/testcases/bitwise/0044payload_binop_2 new file +11 -0
@@ -0,0 +1,11 @@ +#!/bin/bash + +set -e + +RULESET=" + add table t + add chain t c { type filter hook output priority filter; } + add rule t c ct mark set ct mark | ip dscp | 0x200 counter +" + +$NFT -f - <<< "$RULESET"
tests/shell/testcases/bitwise/0044payload_binop_3 new file +11 -0
@@ -0,0 +1,11 @@ +#!/bin/bash + +set -e + +RULESET=" + add table ip6 t + add chain ip6 t c { type filter hook output priority filter; } + add rule ip6 t c ip6 dscp set (ct mark & 0xfc000000) >> 26 +" + +$NFT -f - <<< "$RULESET"
tests/shell/testcases/bitwise/0044payload_binop_4 new file +11 -0
@@ -0,0 +1,11 @@ +#!/bin/bash + +set -e + +RULESET=" + add table ip6 t + add chain ip6 t c { type filter hook output priority filter; } + add rule ip6 t c ip6 dscp set ip6 dscp and 0xc +" + +$NFT -f - <<< "$RULESET"
tests/shell/testcases/bitwise/0044payload_binop_5 new file +11 -0
@@ -0,0 +1,11 @@ +#!/bin/bash + +set -e + +RULESET=" + add table ip6 t + add chain ip6 t c { type filter hook output priority filter; } + add rule ip6 t c ct mark set ct mark | ip6 dscp | 0x200 counter +" + +$NFT -f - <<< "$RULESET"
tests/shell/testcases/bitwise/dumps/0040mark_binop_10.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 ct mark & 0xffff0000 | meta mark & 0x0000ffff + } +}
tests/shell/testcases/bitwise/dumps/0040mark_binop_11.nft new file +6 -0
@@ -0,0 +1,6 @@ +table ip t { + chain c { + type filter hook input priority filter; policy accept; + meta mark set ct mark & 0xffff0000 | meta mark & 0x0000ffff + } +}
tests/shell/testcases/bitwise/dumps/0040mark_binop_12.nft new file +6 -0
@@ -0,0 +1,6 @@ +table ip6 t { + chain c { + type filter hook output priority filter; policy accept; + ct mark set ct mark & 0xffff0000 | meta mark & 0x0000ffff + } +}
tests/shell/testcases/bitwise/dumps/0040mark_binop_13.nft new file +6 -0
@@ -0,0 +1,6 @@ +table ip6 t { + chain c { + type filter hook input priority filter; policy accept; + meta mark set ct mark & 0xffff0000 | meta mark & 0x0000ffff + } +}
tests/shell/testcases/bitwise/dumps/0044payload_binop_0.nft new file +6 -0
@@ -0,0 +1,6 @@ +table ip t { + chain c { + type filter hook output priority filter; policy accept; + ip dscp set (ct mark & 0xfc000000) >> 26 + } +}
tests/shell/testcases/bitwise/dumps/0044payload_binop_1.nft new file +6 -0
@@ -0,0 +1,6 @@ +table ip t { + chain c { + type filter hook output priority filter; policy accept; + ip dscp set ip dscp & af12 + } +}
tests/shell/testcases/bitwise/dumps/0044payload_binop_2.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 ct mark | ip dscp | 0x00000200 counter packets 0 bytes 0 + } +}
tests/shell/testcases/bitwise/dumps/0044payload_binop_3.nft new file +6 -0
@@ -0,0 +1,6 @@ +table ip6 t { + chain c { + type filter hook output priority filter; policy accept; + ip6 dscp set (ct mark & 0xfc000000) >> 26 + } +}
tests/shell/testcases/bitwise/dumps/0044payload_binop_4.nft new file +6 -0
@@ -0,0 +1,6 @@ +table ip6 t { + chain c { + type filter hook output priority filter; policy accept; + ip6 dscp set ip6 dscp & 0x0c + } +}
tests/shell/testcases/bitwise/dumps/0044payload_binop_5.nft new file +6 -0
@@ -0,0 +1,6 @@ +table ip6 t { + chain c { + type filter hook output priority filter; policy accept; + ct mark set ct mark | ip6 dscp | 0x00000200 counter packets 0 bytes 0 + } +}
Signature · 1 line
2.39.2
Quoted 42 lines · Jeremy Sowden
This patch-set adds support for new bitwise boolean operations to nftables, and uses this to extend the types of value which can be assigned to packet marks and payload fields. The original motivation for these changes was Kevin Darbyshire-Bryant's wish to be able to set the conntrack mark to a bitwise expression derived from a DSCP value:
https://lore.kernel.org/netfilter-devel/[email protected]/#r
For example:
nft add rule t c ct mark set ip dscp lshift 26 or
0x10Examples like this could be implemented solely by changes to user space. However, other examples came up in later discussion, such as:
nft add rule t c ct mark set ct mark and
0xffff0000or meta mark and0xffffand most recently:
nft add rule t c ct mark set ct mark or ip dscp or
0x200which require boolean bitwise operations with two variable operands.
Hitherto, the kernel has required that AND, OR and XOR operations be converted in user space to mask-and-xor operations on one register and two immediate values. The related kernel space patch-set, however, adds support for performing these operations directly on one register and an immediate value, or on two registers. This patch-set extends nftables to make use of this functionality.
The previous version of this series also included a few small changes to make it easier to add debug output and changes to support the assign- ments which did not require binops on two registers. The former have been dropped and the latter were reworked and applied by Pablo. The following remain.
* Patch 1 adds support for linearizing and delinearizing the new operations.
* Patches 2-7 add support for using them in payload and mark assignments.
* Patch 8 adds tests for the new assignments.
Quoted 10 lines
Jeremy Sowden (8):
netlink: support (de)linearization of new bitwise boolean operations
netlink_delinearize: refactorstmt_payload_binop_postprocess
netlink_delinearize: add support for processing variable payload
statement arguments
evaluate: prevent nested byte-order conversions
evaluate: preserve existing binop properties
evaluate: allow binop expressions with variable right-hand operands
parser_json: allow RHS mark and payload expressions
tests: add tests for binops with variable RHS operands