From eab3eb7f146c07009ccd16ed41afd2769882fca0 Mon Sep 17 00:00:00 2001 From: Jeremy Sowden Date: Mon, 4 Apr 2022 13:13:52 +0100 Subject: [PATCH] evaluate: relax type-checking for integer arguments in mark statements In order to be able to set ct and meta marks to values derived from payload expressions, we need to relax the requirement that the type of the statement argument must match that of the statement key. Instead, we require that the base-type of the argument is integer and that the argument is small enough to fit. Add one testcase for tests/py. Signed-off-by: Jeremy Sowden --- src/evaluate.c | 8 ++++++-- tests/py/ip/meta.t | 2 ++ tests/py/ip/meta.t.json | 20 ++++++++++++++++++++ tests/py/ip/meta.t.payload | 8 ++++++++ 4 files changed, 36 insertions(+), 2 deletions(-) diff --git a/src/evaluate.c b/src/evaluate.c index 128b75040..fe6384a48 100644 --- a/src/evaluate.c +++ b/src/evaluate.c @@ -2690,8 +2690,12 @@ static int __stmt_evaluate_arg(struct eval_ctx *ctx, struct stmt *stmt, "expression has type %s with length %d", dtype->desc, (*expr)->dtype->desc, (*expr)->len); - else if ((*expr)->dtype->type != TYPE_INTEGER && - !datatype_equal((*expr)->dtype, dtype)) + + if ((dtype->type == TYPE_MARK && + !datatype_equal(datatype_basetype(dtype), datatype_basetype((*expr)->dtype))) || + (dtype->type != TYPE_MARK && + (*expr)->dtype->type != TYPE_INTEGER && + !datatype_equal((*expr)->dtype, dtype))) return stmt_binary_error(ctx, *expr, stmt, /* verdict vs invalid? */ "datatype mismatch: expected %s, " "expression has type %s", diff --git a/tests/py/ip/meta.t b/tests/py/ip/meta.t index 5a05923a1..85eaf54ce 100644 --- a/tests/py/ip/meta.t +++ b/tests/py/ip/meta.t @@ -15,3 +15,5 @@ meta obrname "br0";fail meta sdif "lo" accept;ok meta sdifname != "vrf1" accept;ok + +meta mark set ip dscp;ok diff --git a/tests/py/ip/meta.t.json b/tests/py/ip/meta.t.json index 3df31ce38..a93d7e781 100644 --- a/tests/py/ip/meta.t.json +++ b/tests/py/ip/meta.t.json @@ -156,3 +156,23 @@ } } ] + +# meta mark set ip dscp +[ + { + "mangle": { + "key": { + "meta": { + "key": "mark" + } + }, + "value": { + "payload": { + "field": "dscp", + "protocol": "ip" + } + } + } + } +] + diff --git a/tests/py/ip/meta.t.payload b/tests/py/ip/meta.t.payload index afde5cc13..1aa8d003b 100644 --- a/tests/py/ip/meta.t.payload +++ b/tests/py/ip/meta.t.payload @@ -51,3 +51,11 @@ ip test-ip4 input [ cmp eq reg 1 0x00000011 ] [ payload load 2b @ transport header + 2 => reg 1 ] [ cmp eq reg 1 0x00004300 ] + +# meta mark set ip dscp +ip test-ip4 input + [ payload load 1b @ network header + 1 => reg 1 ] + [ bitwise reg 1 = ( reg 1 & 0x000000fc ) ^ 0x00000000 ] + [ bitwise reg 1 = ( reg 1 >> 0x00000002 ) ] + [ meta set mark with reg 1 ] + -- 2.47.2