meta: random match for statistic sampling
Florian Westphal
maintainercover meta: random match for statistic sampling
Replacement for iptables -m statistic 'random' mode.
I added it to meta match even though its not directly related to an skb member. We already have cpu match which isn't related to skb either and adding a new expression just for this seemed overkill.
If you'd rather like a new prandom expression just let me know and I can rework it.
There is also a libnftnl patch but its only so that debug output displays 'meta prandom' rather than 'meta unknown'.
Result after several minutes of constant flooding:
counter packets 2961796882 bytes 248790938088
meta random <= 0.900000000 counter packets 2665649546 bytes 223914561864 meta random <= 0.500000000 counter packets 1480856860 bytes 124391976240 meta random <= 0.100000000 counter packets 296190072 bytes 24879966048 meta random <= 0.010000000 counter packets 29618610 bytes 2487963240 meta random <= 0.001000000 counter packets 2963660 bytes 248947440 meta random <= 0.000100000 counter packets 295486 bytes 24820824 meta random <= 0.000010000 counter packets 29450 bytes 2473800 meta random <= 0.000001000 counter packets 2911 bytes 244524 meta random <= 0.000000100 counter packets 267 bytes 22428 meta random <= 0.000000010 counter packets 26 bytes 2184 meta random <= 0.000000001 counter packets 4 bytes 336
... so it seems to work as intended.
Let me know if you spot any issues with current approach.
Kernel part only does '*dest = prandom_u32()', I'll submit it once I know that this approach is deemed sane.
Thanks!
Florian Westphal
maintainerPATCH 1/3 evaluate: move default op lookup into helper
Split it into its own function, caller is already quite big.
--- src/evaluate.c | 43 ++++++++++++++++++++++--------------------- 1 file changed, 22 insertions(+), 21 deletions(-)
src/evaluate.c +22 -21
@@ -1119,6 +1119,26 @@ static int binop_transfer(struct eval_ctx *ctx, struct expr **expr)return 0 ; } +static enum ops expr_defaultop (const struct expr *e) +{ +switch (e->ops->type) { +case EXPR_RANGE : +return OP_RANGE ; +case EXPR_SET : +case EXPR_SET_REF : +return OP_LOOKUP ; +case EXPR_LIST : +return OP_FLAGCMP ; +default : +if (e->dtype->basetype !=NULL && + e->dtype->basetype->type ==TYPE_BITMASK ) +return OP_FLAGCMP ; +break ; + } + +return OP_EQ ; +} +static int expr_evaluate_relational (struct eval_ctx *ctx,struct expr **expr) {struct expr *rel = *expr, *left, *right;
@@ -1131,27 +1151,8 @@ static int expr_evaluate_relational(struct eval_ctx *ctx, struct expr **expr)return -1 ; right = rel->right; -if (rel->op ==OP_IMPLICIT ) { -switch (right->ops->type) { -case EXPR_RANGE : - rel->op =OP_RANGE ; -break ; -case EXPR_SET : -case EXPR_SET_REF : - rel->op =OP_LOOKUP ; -break ; -case EXPR_LIST : - rel->op =OP_FLAGCMP ; -break ; -default : -if (right->dtype->basetype !=NULL && - right->dtype->basetype->type ==TYPE_BITMASK ) - rel->op =OP_FLAGCMP ; -else - rel->op =OP_EQ ; -break ; - } - } +if (rel->op ==OP_IMPLICIT ) + rel->op =expr_defaultop (rel->right);if (!expr_is_constant (right)) return expr_binary_error(ctx->msgs, right, rel,
Signature · 1 line
2.4.10
Florian Westphal
maintainerPATCH 2/3 meta: add prandom matching
In reply to Florian WestphalProbalistic random matching just like iptables
-m statistic --mode random --probability 0.1.
It introduces a new 'probability' type, which looks like fractional value ("0.25") to the user, but is internally scaled to a uint32_t where 1.0 is UINT_MAX.
Furthermore, make the default op for the type <= so that "nft ... meta random 0.5" will match every 2nd packet.
This means that users can still do something like
"nft ... meta random eq 0.1" although thats not very useful (matching probability 1/UINT_MAX).
--- doc/nft.xml | 14 ++++++++++ include/datatype.h | 2 ++ include/linux/netfilter/nf_tables.h | 2 ++ src/evaluate.c | 3 +++ src/meta.c | 54 +++++++++++++++++++++++++++++++++++++ src/parser_bison.y | 1 + src/scanner.l | 6 ++++- 7 files changed, 81 insertions(+), 1 deletion(-)
doc/nft.xml +14 -0
@@ -966,6 +966,7 @@ filter output ip6 daddr ::1 <arg>l4proto</arg> <arg>protocol</arg> <arg>priority</arg> + <arg>random</arg> </group> </cmdsynopsis> <cmdsynopsis>
@@ -1032,6 +1033,11 @@ filter output ip6 daddr ::1 <entry>integer (32 bit)</entry> </row> <row> + <entry>random</entry> + <entry>probability</entry> + <entry>probability_type</entry> + </row> + <row> <entry>mark</entry> <entry>Packet mark</entry> <entry>packetmark</entry>
@@ -1187,6 +1193,14 @@ filter output ip6 daddr ::1 Broadcast (to all), Multicast (to group). </entry> </row> + <row> + <entry>probability_type</entry> + <entry> + Probability type: floating point value, must be within 0.0 and 0.9999999. + The value given is internally mapped to a 32bit integer number. + </entry> + </row> + </tbody> </tgroup> </table>
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, };/**
src/evaluate.c +3 -0
@@ -1133,6 +1133,9 @@ static enum ops expr_defaultop(const struct expr *e)if (e->dtype->basetype !=NULL && e->dtype->basetype->type ==TYPE_BITMASK )return OP_FLAGCMP ; + +if (e->dtype->type ==TYPE_PROBABILITY ) +return OP_LTE ;break ; }
src/meta.c +54 -0
78 lines not on this page · raw message
src/parser_bison.y +1 -0
@@ -2235,6 +2235,7 @@ meta_key_qualified : LENGTH { $$ = NFT_META_LEN; } | L4PROTO { $$ = NFT_META_L4PROTO; } | PROTOCOL { $$ = NFT_META_PROTOCOL; } | PRIORITY { $$ = NFT_META_PRIORITY; } + | RANDOM { $$ = NFT_META_PRANDOM; } ; meta_key_unqualified : MARK { $$ = NFT_META_MARK; }
src/scanner.l +5 -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}|[/\-_\.])*
@@ -486,7 +487,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
Florian Westphal
maintainerPATCH 3/3 tests: add test cases for meta random
In reply to Florian Westphal--- tests/py/any/meta.t | 6 ++++++ tests/py/any/meta.t.payload | 19 +++++++++++++++++++ 2 files changed, 25 insertions(+)
tests/py/any/meta.t +6 -0
@@ -197,3 +197,9 @@ 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 random 0.999999999;ok;meta random <= 0.999999999 +meta random 0.5;ok;meta random <= 0.500000000 +meta random 0.001;ok;meta random <= 0.001000000 +meta random 0.000000001;ok;meta random <= 0.000000001 +meta random 0.0000000005;fail
tests/py/any/meta.t.payload +19 -0
@@ -754,3 +754,22 @@ ip test-ip4 output [ meta load oif => reg 9 ] [ lookup reg 1 set map%d dreg 0 ] +# meta random 0.999999999 +ip test-ip4 input + [ meta load prandom => reg 1 ] + [ cmp lte reg 1 0xfbffffff ] + +# meta random 0.5 +ip test-ip4 input + [ meta load prandom => reg 1 ] + [ cmp lte reg 1 0x00000080 ] + +# meta random 0.001 +ip test-ip4 input + [ meta load prandom => reg 1 ] + [ cmp lte reg 1 0x37894100 ] + +# meta random 0.000000001 +ip test-ip4 input + [ meta load prandom => reg 1 ] + [ cmp lte reg 1 0x04000000 ]
Signature · 1 line
2.4.10
Pablo Neira Ayuso
maintainerQuoted 26 lines · Florian Westphal
Replacement for iptables -m statistic 'random' mode.
I added it to meta match even though its not directly related to an skb member. We already have cpu match which isn't related to skb either and adding a new expression just for this seemed overkill.If you'd rather like a new prandom expression just let me know and I can rework it.
There is also a libnftnl patch but its only so that debug output displays 'meta prandom' rather than 'meta unknown'.
Result after several minutes of constant flooding:
counter packets 2961796882 bytes 248790938088
meta random <= 0.900000000 counter packets 2665649546 bytes 223914561864 meta random <= 0.500000000 counter packets 1480856860 bytes 124391976240 meta random <= 0.100000000 counter packets 296190072 bytes 24879966048 meta random <= 0.010000000 counter packets 29618610 bytes 2487963240 meta random <= 0.001000000 counter packets 2963660 bytes 248947440 meta random <= 0.000100000 counter packets 295486 bytes 24820824 meta random <= 0.000010000 counter packets 29450 bytes 2473800 meta random <= 0.000001000 counter packets 2911 bytes 244524 meta random <= 0.000000100 counter packets 267 bytes 22428 meta random <= 0.000000010 counter packets 26 bytes 2184 meta random <= 0.000000001 counter packets 4 bytes 336
Quoted 6 lines
... so it seems to work as intended.
Let me know if you spot any issues with current approach.
Kernel part only does '*dest =
prandom_u32()', I'll submit it once I know that this approach is deemed sane.
Fine with me.
I also started a patch to add nth and jhash support, but it's incomplete. Laura (she's on Cc) also wanted to have these for her work on modeling load balancer schedulers with nft.
Thanks.
Re: [PATCH nft 2/3] meta: add prandom matching
In reply to Florian WestphalAm 2. Februar 2016 00:19:00 MEZ, schrieb Florian Westphal <[email protected]>:
include/datatype.h enum datatypes { · line 84
Quoted 10 lines
enum datatypes {
TYPE_INVALID,include/datatype.h +1
-0@@ -78,6 +79,7 @@ enum datatypes {TYPE_ICMPV6_CODE ,TYPE_ICMPX_CODE ,TYPE_DEVGROUP , +TYPE_PROBABILITY , __TYPE_MAX
Any reason why you chose to add this type instead of a generic floating point type?
Florian Westphal
maintainerRe: [PATCH nft 2/3] meta: add prandom matching
Quoted 13 lines · Patrick McHardy
Am 2. Februar 2016 00:19:00 MEZ, schrieb Florian Westphal <[email protected]>:
include/datatype.h enum datatypes { · line 84
Quoted 10 lines
enum datatypes {
TYPE_INVALID,include/datatype.h +1
-0@@ -78,6 +79,7 @@ enum datatypes {TYPE_ICMPV6_CODE ,TYPE_ICMPX_CODE ,TYPE_DEVGROUP , +TYPE_PROBABILITY , __TYPE_MAXAny reason why you chose to add this type instead of a generic floating point type?
I wanted 0.9999 be tranlated to a value close to UINT32_MAX and 0.00001 to something close to zero so that "meta random 0.999" can be translated to something like
reg1 = prandom_u32()
reg1 <= 0xffffffee
I.e. this type cannot represent 5.2 (or whatever).
Does that answer your question?
Re: [PATCH nft 2/3] meta: add prandom matching
Quoted 19 lines · Florian Westphal
Quoted 13 lines · Patrick McHardy
Am 2. Februar 2016 00:19:00 MEZ, schrieb Florian Westphal <[email protected]>:
include/datatype.h enum datatypes { · line 84
Quoted 10 lines
enum datatypes {
TYPE_INVALID,include/datatype.h +1
-0@@ -78,6 +79,7 @@ enum datatypes {TYPE_ICMPV6_CODE ,TYPE_ICMPX_CODE ,TYPE_DEVGROUP , +TYPE_PROBABILITY , __TYPE_MAXAny reason why you chose to add this type instead of a generic floating point type?
I wanted 0.9999 be tranlated to a value close to
UINT32_MAXand 0.00001 to something close to zero so that "meta random 0.999" can be translated to something like
Quoted 6 lines
reg1 =
prandom_u32()
reg1 <=0xffffffeeI.e. this type cannot represent 5.2 (or whatever).
Does that answer your question?
Not really unless I'm misunderstanding your intention. That part is related to the kernel internal representation and could be handled during linearization.
Florian Westphal
maintainerRe: [PATCH nft 2/3] meta: add prandom matching
Quoted 9 lines · Patrick McHardy
Quoted 9 lines
Quoted 1 line
Any reason why you chose to add this type instead of a generic floating point type?
I wanted 0.9999 be tranlated to a value close to
UINT32_MAXand 0.00001 to something close to zero so that "meta random 0.999" can be translated to something likereg1 =
prandom_u32()
reg1 <=0xffffffee
Quoted 7 lines
Quoted 3 lines
I.e. this type cannot represent 5.2 (or whatever).
Does that answer your question?
Not really unless I'm misunderstanding your intention. That part is related to the kernel internal representation and could be handled during linearization.
So what would you suggest?
Add support for translating double to mpz_t? What precisions would you support?
How to handle the scaling at (de)linearization time if type doesn't do that aynmore?
What should happen when user asks for meta random 42.23 ?
Florian Westphal
maintainerRe: [PATCH nft 2/3] meta: add prandom matching
Quoted 14 lines · Florian Westphal
Quoted 13 lines · Patrick McHardy
Quoted 12 lines
Quoted 1 line
Any reason why you chose to add this type instead of a generic floating point type?
I wanted 0.9999 be tranlated to a value close to
UINT32_MAXand 0.00001 to something close to zero so that "meta random 0.999" can be translated to something likereg1 =
prandom_u32()
reg1 <=0xffffffeeI.e. this type cannot represent 5.2 (or whatever).
Does that answer your question?
Quoted 7 lines
Quoted 3 lines
Not really unless I'm misunderstanding your intention. That part is related to the kernel internal representation and could be handled during linearization.
So what would you suggest?
Add support for translating double tompz_t?
What precisions would you support?
So I've started to generalize the proposed precision type into type_float which would support 0.000000001 as smallest value.
Does that seem ok or would you use a different precision? (If so, what & why?)
Thanks!
Quoted 1 line
What should happen when user asks for meta random 42.23 ?
That still stands, where would this error be detect best?
Florian Westphal
maintainerRe: [PATCH nft 2/3] meta: add prandom matching
Quoted 26 lines · Florian Westphal
Quoted 14 lines · Florian Westphal
Quoted 13 lines · Patrick McHardy
Quoted 12 lines
Quoted 1 line
Any reason why you chose to add this type instead of a generic floating point type?
I wanted 0.9999 be tranlated to a value close to
UINT32_MAXand 0.00001 to something close to zero so that "meta random 0.999" can be translated to something likereg1 =
prandom_u32()
reg1 <=0xffffffeeI.e. this type cannot represent 5.2 (or whatever).
Does that answer your question?
Quoted 7 lines
Quoted 3 lines
Not really unless I'm misunderstanding your intention. That part is related to the kernel internal representation and could be handled during linearization.
So what would you suggest?
Add support for translating double tompz_t?
What precisions would you support?So I've started to generalize the proposed precision type into
type_floatwhich would support 0.000000001 as smallest value.
Quoted 6 lines
Does that seem ok or would you use a different precision? (If so, what & why?)
Quoted 1 line
What should happen when user asks for meta random 42.23 ?
That still stands, where would this error be detect best?
I've also been unable to figure out where the u32 scaling has to be performed, netlink_linearize.c seems to be a very poor place for this since it would mean special-casing netlink_gen_cmp to check for OP_LTE and presence of EXPR_META with the meta PRANDOM key...?
In fact, doing the scaling via precision_type seems to be a lot simpler as then its applied only in this one case of the prandom META_TEMPLATE while keeping this detail limited to meta.c.
Re: [PATCH nft 2/3] meta: add prandom matching
In reply to Florian WestphalQuoted 19 lines · Florian Westphal
Quoted 18 lines · Florian Westphal
Quoted 16 lines · Patrick McHardy
Quoted 6 lines
Quoted 1 line
Any reason why you chose to add this type instead of a generic floating point type?
I wanted 0.9999 be tranlated to a value close to
UINT32_MAXand 0.00001 to something close to zero so that "meta random 0.999" can be translated to something likeQuoted 6 lines
reg1 =
prandom_u32()
reg1 <=0xffffffeeI.e. this type cannot represent 5.2 (or whatever).
Does that answer your question?
Not really unless I'm misunderstanding your intention. That part is related to the kernel internal representation and could be handled during linearization.
Quoted 6 lines
Quoted 3 lines
So what would you suggest?
Add support for translating double tompz_t?
What precisions would you support?So I've started to generalize the proposed precision type into
type_floatwhich would support 0.000000001 as smallest value.
Sounds good.
Quoted 2 lines
Does that seem ok or would you use a different precision? (If so, what & why?)
Seems reasonable for this case as 0.000000000232 is the smallest value in 32 bit representation.
Quoted 5 lines
Thanks!
Quoted 1 line
What should happen when user asks for meta random 42.23 ?
That still stands, where would this error be detect best?
It would be nice to handle this in expr_evaluate_value() by propagating the limit from the RHS somehow. Right now we base our limits on the available bits, we'd also need a numeric limit for this case.
I guess the question is related to how we're going to do the mapping to 32 bit integers. Define a global mapping that is valid for all floats? Then the normal bit based range check would work. Not sure if that makes sense though.
You're way might have been the better one after all.
Re: [PATCH nft 2/3] meta: add prandom matching
In reply to Florian WestphalQuoted 12 lines · Florian Westphal
Quoted 3 lines · Florian Westphal
Quoted 1 line
What should happen when user asks for meta random 42.23 ?
That still stands, where would this error be detect best?
I've also been unable to figure out where the u32 scaling has to be performed,
netlink_linearize.cseems to be a very poor place for this since it would mean special-casingnetlink_gen_cmpto check forOP_LTEand presence ofEXPR_METAwith the meta PRANDOM key...?
Quoted 3 lines
In fact, doing the scaling via
precision_typeseems to be a lot simpler as then its applied only in this one case of the prandomMETA_TEMPLATEwhile keeping this detail limited tometa.c.
Yes, on second thought I agree, sorry. Maybe the work is not lost though, what does seem to make sense is to use a float basetype and derive your probability type from that.
Florian Westphal
maintainerRe: [PATCH nft 2/3] meta: add prandom matching
Patrick McHardy <[email protected]> wrote:
Hi Patrick
Quoted 8 lines · Florian Westphal
Quoted 3 lines
In fact, doing the scaling via
precision_typeseems to be a lot simpler as then its applied only in this one case of the prandomMETA_TEMPLATEwhile keeping this detail limited tometa.c.Yes, on second thought I agree, sorry. Maybe the work is not lost though, what does seem to make sense is to use a float basetype and derive your probability type from that.
I can do this.
However, I don't currently see any other type that could be derived from that. Would you be OK with leaving things as-is and adding a float type later on once a use case presents itself?
I would re-word the commit message to mention the 'missing' float base type.
Thanks!
Pablo Neira Ayuso
maintainerRe: [PATCH nft 2/3] meta: add prandom matching
Quoted 9 lines · Florian Westphal
Patrick McHardy <[email protected]> wrote:
Hi Patrick
Quoted 5 lines · Florian Westphal
Quoted 3 lines
In fact, doing the scaling via
precision_typeseems to be a lot simpler as then its applied only in this one case of the prandomMETA_TEMPLATEwhile keeping this detail limited tometa.c.
Quoted 8 lines
Quoted 3 lines
Yes, on second thought I agree, sorry. Maybe the work is not lost though, what does seem to make sense is to use a float basetype and derive your probability type from that.
I can do this.
However, I don't currently see any other type that could be derived from that. Would you be OK with leaving things as-is and adding a float type later on once a use case presents itself?
I also think you can add a new TYPE_FLOAT.
Then, from the evaluation step make sure that META_PRANDOM is under the valid limits (0, 1].
These TYPE_* will be part of the public API of the high level library at some point, they describe the datatype that are used in set definitions in the kernel (through the NFTA_SET_DATATYPE netlink attribute and the new NFTA_SET_USERDATA through TLVs).
Florian Westphal
maintainerRe: [PATCH nft 2/3] meta: add prandom matching
Quoted 14 lines · Pablo Neira Ayuso
Quoted 13 lines · Florian Westphal
Patrick McHardy <[email protected]> wrote:
Hi Patrick
Quoted 8 lines · Florian Westphal
Quoted 3 lines
In fact, doing the scaling via
precision_typeseems to be a lot simpler as then its applied only in this one case of the prandomMETA_TEMPLATEwhile keeping this detail limited tometa.c.Yes, on second thought I agree, sorry. Maybe the work is not lost though, what does seem to make sense is to use a float basetype and derive your probability type from that.
Quoted 6 lines
Quoted 4 lines
I can do this.
However, I don't currently see any other type that could be derived from that. Would you be OK with leaving things as-is and adding a float type later on once a use case presents itself?I also think you can add a new
TYPE_FLOAT.
Yes, but what I'm asking is: 'What for'?
Quoted 2 lines
Then, from the evaluation step make sure that
META_PRANDOMis under the valid limits (0, 1].
Thats not so simple. META_PRANDOM is scaled so its represented as 1.0 == UINT32_MAX and 0 = 0.
We can't do that for TYPE_FLOAT since it means that it could not represent values > 1.0 .
Doing the scaling in the eval step is possible but its a bit ugly.
Quoted 4 lines
These TYPE_* will be part of the public API of the high level library at some point, they describe the datatype that are used in set definitions in the kernel (through the
NFTA_SET_DATATYPEnetlink attribute and the newNFTA_SET_USERDATAthrough TLVs).
I understand, but, the proposed float and probability types are very different, and allow for very little re-use.
For example on printing the probability type has to undo the scaling so we print 1.0 instead of $bignum.
Pablo Neira Ayuso
maintainerRe: [PATCH nft 2/3] meta: add prandom matching
Quoted 2 lines · Florian Westphal
I understand, but, the proposed float and probability types are very different, and allow for very little re-use.
I see, then stick to the probability datatype.