expression: add helper to decide if operator needs to be shown
Florian Westphal
maintainerPATCH 1/3 expression: add helper to decide if operator needs to be shown
tcp dport 22 is treated as if user had given 'tcp dport == 22'. When printing, the implicit == is omitted.
In some other cases we use OP_AND instead, e.g.
tcp flags ack means 'tcp flags & ack != 0'.
In all of these cases, we print the rule in the short form, without showing this implicit operator.
Future patches will add other cases where an operator other than AND or EQ can be suppressed, so add an explicit helper that can suppress the operator symbol.
--- src/expression.c | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-)
src/expression.c +13 -7
@@ -514,21 +514,27 @@ static void binop_arg_print(const struct expr *op, const struct expr *arg)printf (")" ); } -static bool must_print_eq_op (const struct expr *expr) +static bool must_print_op (const struct expr *binop) { -if (expr->right->dtype->basetype !=NULL && - expr->right->dtype->basetype->type ==TYPE_BITMASK ) -return true; +switch (binop->op) { +case OP_EQ : +if (binop->right->dtype->basetype !=NULL && + binop->right->dtype->basetype->type ==TYPE_BITMASK ) +return true; -return expr->left->ops->type ==EXPR_BINOP ; +return binop->left->ops->type ==EXPR_BINOP ; +default : +break ; + } + +return true; }static void binop_expr_print (const struct expr *expr) {binop_arg_print (expr, expr->left); -if (expr_op_symbols[expr->op] && - (expr->op !=OP_EQ ||must_print_eq_op (expr))) +if (expr_op_symbols[expr->op] &&must_print_op (expr))printf (" %s " , expr_op_symbols[expr->op]);else printf (" " );
Signature · 1 line
2.4.10
Florian Westphal
maintainerPATCH 3/4 meta: add probability matching
nft meta probability 0.5
probalistic matching just like iptables
'-m statistic --mode random --probability 0.5':
Internally nft translates the request to this:
[ meta load prandom => reg 1 ]
[ cmp lte reg 1 0xffffff7f ]
but this stays hidden from the user (i.e. <= operator is not shown on list).
The float value has to be in range of 0.0000001 to 0.9999999 and is internally scaled from 0 to UINT_MAX (the higher the value, the higher the likelyhood of 'random() <= value' being true).
This patch deliberately doesn't add the META_PRANDOM key to the existing meta keys -- this way we do not allow statement like 'meta probability ne 0.2' since parser will expect a probability value instead of 'ne'.
---
NB: If you still dislike TYPE_PROBABILITY it would be possible to handle the de-scaling during netlink delinearization.
This would also allow us to zap the relational expression at the same time which in turn avoids the code to suppress OP_LTE for the probability case.
Only problem is that it needs a bit of meta.c details in netlink_delinearize (can only do the re-scaling in case of RELOP w. OP_LTE && left-type-is-meta && meta-key-is-prandom).
Thoughts?
include/datatype.h | 2 ++ include/linux/netfilter/nf_tables.h | 2 ++ include/meta.h | 4 +++ src/expression.c | 2 ++ src/meta.c | 70 +++++++++++++++++++++++++++++++++++++ src/parser_bison.y | 24 +++++++++++-- src/scanner.l | 7 +++- 7 files changed, 108 insertions(+), 3 deletions(-)
include/datatype.h +2 -0
@@ -40,6 +40,7 @@* @TYPE_ICMPV6_CODE: icmpv6 code (integer subtype) * @TYPE_ICMPX_CODE: icmpx code (integer subtype) * @TYPE_DEVGROUP: devgroup code (integer subtype) +* @TYPE_PROBABILITY: probability value (integer subtype) */ enum datatypes {TYPE_INVALID ,
@@ -78,6 +79,7 @@ enum datatypes {TYPE_ICMPV6_CODE ,TYPE_ICMPX_CODE ,TYPE_DEVGROUP , +TYPE_PROBABILITY , __TYPE_MAX };#define TYPE_MAX (__TYPE_MAX - 1)
include/linux/netfilter/nf_tables.h +2 -0
@@ -668,6 +668,7 @@ enum nft_exthdr_attributes {* @NFT_META_IIFGROUP: packet input interface group * @NFT_META_OIFGROUP: packet output interface group * @NFT_META_CGROUP: socket control group (skb->sk->sk_classid) +* @NFT_META_PRANDOM: a 32bit pseudo-random number */ enum nft_meta_keys {NFT_META_LEN ,
@@ -694,6 +695,7 @@ enum nft_meta_keys {NFT_META_IIFGROUP ,NFT_META_OIFGROUP ,NFT_META_CGROUP , + NFT_META_PRANDOM, };/**
include/meta.h +4 -0
@@ -26,6 +26,10 @@ struct meta_template {extern struct expr *meta_expr_alloc (const struct location *loc,enum nft_meta_keys key); +struct error_record *meta_probability_parse (const struct location *loc, +const char *s,uint32_t *v); +struct expr *meta_expr_alloc_probability (const struct location *loc,uint32_t p); +struct stmt *meta_stmt_meta_iiftype (const struct location *loc,uint16_t type);const struct datatype ifindex_type;
src/expression.c +2 -0
@@ -523,6 +523,8 @@ static bool must_print_op(const struct expr *binop)return true;return binop->left->ops->type ==EXPR_BINOP ; +case OP_LTE : +return binop->left->dtype->type !=TYPE_PROBABILITY ;default :break ; }
src/meta.c +70 -0
111 lines not on this page · raw message
src/parser_bison.y +22 -2
@@ -329,6 +329,7 @@ static void location_update(struct location *loc, struct location *rhs, int n) %token IBRIPORT "ibriport" %token OBRIPORT "obriport" %token PKTTYPE "pkttype" +%token PROBABILITY "probability" %token CPU "cpu" %token IIFGROUP "iifgroup" %token OIFGROUP "oifgroup"
@@ -563,8 +564,8 @@ static void location_update(struct location *loc, struct location *rhs, int n) %destructor { expr_free($$); } mh_hdr_expr %type <val> mh_hdr_field -%type <expr> meta_expr -%destructor { expr_free($$); } meta_expr +%type <expr> meta_expr meta_probability_expr +%destructor { expr_free($$); } meta_expr meta_probability_expr %type <val> meta_key meta_key_qualified meta_key_unqualified %type <expr> ct_expr
@@ -1764,6 +1765,10 @@ match_stmt : relational_expr { $$ = expr_stmt_alloc(&@$, $1); } + | meta_probability_expr + { + $$ = expr_stmt_alloc(&@$, $1); + } ; symbol_expr : string
@@ -2226,6 +2231,21 @@ meta_expr : META meta_key } ; +meta_probability_expr : META PROBABILITY STRING + { + struct error_record *erec; + uint32_t value; + + erec = meta_probability_parse(&@$, $3, &value); + if (erec != NULL) { + erec_queue(erec, state->msgs); + YYERROR; + } + + $$ = meta_expr_alloc_probability(&@$, value); + } + ; + meta_key : meta_key_qualified | meta_key_unqualified ;
src/scanner.l +6 -1
@@ -110,6 +110,7 @@ digit [0-9] hexdigit [0-9a-fA-F] decstring {digit}+ hexstring 0[xX]{hexdigit}+ +probability 0.{decstring} range ({decstring}?:{decstring}?) letter [a-zA-Z] string ({letter})({letter}|{digit}|[/\-_\.])*
@@ -445,6 +446,7 @@ addrstring ({macaddr}|{ip4addr}|{ip6addr}) "ibriport" { return IBRIPORT; } "obriport" { return OBRIPORT; } "pkttype" { return PKTTYPE; } +"probability" { return PROBABILITY; } "cpu" { return CPU; } "iifgroup" { return IIFGROUP; } "oifgroup" { return OIFGROUP; }
@@ -486,7 +488,10 @@ addrstring ({macaddr}|{ip4addr}|{ip6addr}) } return NUM; } - +{probability} { + yylval->string = xstrdup(yytext); + return STRING; + } {hexstring} { errno = 0; yylval->val = strtoull(yytext, NULL, 0);
Signature · 1 line
2.4.10
Re: [PATCH nft 3/4] meta: add probability matching
src/meta.c static const struct datatype devgroup_type = { · line 407
Quoted 4 lines · Florian Westphal
The float value has to be in range of 0.0000001 to 0.9999999 and
src/meta.c +3
-0+ +if (tmp >=UINT_MAX || d >0.9999999 ) +return error (loc,"Probability " META_PROB_FMT " too %s" , d,"big" );
You are misappropriating some 430 points here. 0.9999999 maps to just 0xfffffe51.
src/meta.c static const struct datatype devgroup_type = { · line 411
Quoted 3 lines
src/meta.c +3
-0+ *value = (uint32_t ) tmp; +if (*value ==0 ) +return error (loc,"Probability " META_PROB_FMT " too %s" , d,"small" );
Though d==0 and d==1 are not overly useful, why prohibit them? iptables allowed taking them, and I hear people are working on a translator...*boom*
Florian Westphal
maintainerRe: [PATCH nft 3/4] meta: add probability matching
Quoted 9 lines · Jan Engelhardt
src/meta.c static const struct datatype devgroup_type = { · line 407
Quoted 4 lines · Florian Westphal
The float value has to be in range of 0.0000001 to 0.9999999 and
src/meta.c +3
-0+ +if (tmp >=UINT_MAX || d >0.9999999 ) +return error (loc,"Probability " META_PROB_FMT " too %s" , d,"big" );You are misappropriating some 430 points here. 0.9999999 maps to just
0xfffffe51.
Sorry, I am not following.
What would you likew to change here?
Quoted 6 lines
src/meta.c static const struct datatype devgroup_type = { · line 411
Quoted 3 lines
src/meta.c +3
-0+ *value = (uint32_t ) tmp; +if (*value ==0 ) +return error (loc,"Probability " META_PROB_FMT " too %s" , d,"small" );Though d==0 and d==1 are not overly useful, why prohibit them? iptables allowed taking them, and I hear people are working on a translator...*boom*
No, thats easy to translate to nft :-)
1.0 -> ""
0.0 -> "#"
i.e. not print a "1.0" match, its always true so irrelevant and print comment a sign (#) to make nft ignore whatever comes after it, since it did not match in the iptables rule either.
Re: [PATCH nft 3/4] meta: add probability matching
Quoted 11 lines · Florian Westphal
Quoted 9 lines · Jan Engelhardt
src/meta.c static const struct datatype devgroup_type = { · line 407
Quoted 4 lines · Florian Westphal
The float value has to be in range of 0.0000001 to 0.9999999 and
src/meta.c +3
-0+ +if (tmp >=UINT_MAX || d >0.9999999 ) +return error (loc,"Probability " META_PROB_FMT " too %s" , d,"big" );You are misappropriating some 430 points here. 0.9999999 maps to just
0xfffffe51.
Quoted 1 line
Sorry, I am not following.
0.9999999997 is still a valid probability, but you are making it impossible to select it by cutting off at d > 0.9999999000.
Quoted 1 line
What would you likew to change here?
Change to
if (tmp >= UINT_MAX || d >= 1)
(or better yet, d > 1)
Florian Westphal
maintainerRe: [PATCH nft 3/4] meta: add probability matching
Quoted 10 lines · Jan Engelhardt
Quoted 7 lines
Quoted 5 lines
src/meta.c static const struct datatype devgroup_type = { · line 407
Quoted 2 lines
src/meta.c +2
-0+if (tmp >=UINT_MAX || d >0.9999999 ) +return error (loc,"Probability " META_PROB_FMT " too %s" , d,"big" );You are misappropriating some 430 points here. 0.9999999 maps to just
0xfffffe51.Sorry, I am not following.
0.9999999997 is still a valid probability, but you are making it impossible to select it by cutting off at d > 0.9999999000.
I'll change it to allow one more digit, so that supported range will be 0.00000001 (0x0000002a) to
0.99999999 (0xffffffd4).
(I'll wait a bit in case anyone has opinion where to suppress display of
LTE op and where to handle de-scaling of the kernel side integer back to 0.12345 output).
Re: [PATCH nft 3/4] meta: add probability matching
Quoted 7 lines · Florian Westphal
Quoted 6 lines · Jan Engelhardt
Quoted 6 lines
Quoted 5 lines
src/meta.c static const struct datatype devgroup_type = { · line 407
Quoted 2 lines
src/meta.c +2
-0+if (tmp >=UINT_MAX || d >0.9999999 ) +return error (loc,"Probability " META_PROB_FMT " too %s" , d,"big" );You are misappropriating some 430 points here. 0.9999999 maps to just
0xfffffe51.
Quoted 8 lines
Quoted 4 lines
Quoted 1 line
Sorry, I am not following.
0.9999999997 is still a valid probability, but you are making it impossible to select it by cutting off at d > 0.9999999000.
I'll change it to allow one more digit, so that supported range will be 0.00000001 [10^-8] (
0x0000002a) to
0.99999999 (0xffffffd4).
Let's ask differently. What precludes the use of the full range (0x0/0x1) [2*10^-10] to (0xffffffff)?
Florian Westphal
maintainerRe: [PATCH nft 3/4] meta: add probability matching
Quoted 13 lines · Jan Engelhardt
Quoted 12 lines · Florian Westphal
Quoted 10 lines · Jan Engelhardt
Quoted 7 lines
Quoted 5 lines
src/meta.c static const struct datatype devgroup_type = { · line 407
Quoted 2 lines
src/meta.c +2
-0+if (tmp >=UINT_MAX || d >0.9999999 ) +return error (loc,"Probability " META_PROB_FMT " too %s" , d,"big" );You are misappropriating some 430 points here. 0.9999999 maps to just
0xfffffe51.Sorry, I am not following.
0.9999999997 is still a valid probability, but you are making it impossible to select it by cutting off at d > 0.9999999000.
Quoted 6 lines
Quoted 3 lines
I'll change it to allow one more digit, so that supported range will be 0.00000001 [10^-8] (
0x0000002a) to
0.99999999 (0xffffffd4).Let's ask differently. What precludes the use of the full range (0x0/0x1) [2*10^-10] to (
0xffffffff)?
Mapping smallest probability to 0x1 and largest one to 0xffffffff means I lose ability to print back a number as entered.
Florian Westphal
maintainerPATCH 4/4 meta: add tests for probability matching
In reply to Florian Westphal--- tests/py/any/meta.t | 16 ++++++++++++++++ tests/py/any/meta.t.payload | 15 +++++++++++++++ 2 files changed, 31 insertions(+)
tests/py/any/meta.t +16 -0
@@ -197,3 +197,19 @@ meta cgroup {1048577-1048578};ok;cgroup { 1048577-1048578} meta iif . meta oif { lo . eth0 };ok meta iif . meta oif . meta mark { lo . eth0 . 0x0000000a };ok meta iif . meta oif vmap { lo . eth0 : drop };ok + +meta probability 0.0000001;ok +meta probability 0.5;ok;meta probability 0.5000000 +meta probability 0.9999999;ok + +# outside of supported precision range +meta probability 0.00000001;fail +meta probability 0.99999999;fail + +# meta probability cannot be used with operators +meta probability eq 0.5;fail + +# not 0.xxx format +meta probability 1.5;fail +meta probability foobar;fail +meta probability 0;fail
tests/py/any/meta.t.payload +15 -0
@@ -754,3 +754,18 @@ ip test-ip4 output [ meta load oif => reg 9 ] [ lookup reg 1 set map%d dreg 0 ] +# meta probability 0.0000001 +ip test-ip4 input + [ meta load prandom => reg 1 ] + [ cmp lte reg 1 0xad010000 ] + +# meta probability 0.5 +ip test-ip4 input + [ meta load prandom => reg 1 ] + [ cmp lte reg 1 0xffffff7f ] + +# meta probability 0.9999999 +ip test-ip4 input + [ meta load prandom => reg 1 ] + [ cmp lte reg 1 0x51feffff ] +
Signature · 1 line
2.4.10