netfilter-devel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Florian Westphal <fw@strlen.de>
To: <netfilter-devel@vger.kernel.org>
Cc: Florian Westphal <fw@strlen.de>
Subject: [PATCH nft 3/3] netlink_delinearize, meta: show meta prandom <= value as probability mnemonic
Date: Tue,  5 Jul 2016 09:35:35 +0200	[thread overview]
Message-ID: <1467704135-9154-4-git-send-email-fw@strlen.de> (raw)
In-Reply-To: <1467704135-9154-1-git-send-email-fw@strlen.de>

During delinearization, check if we're looking at an expression
statement of the form

	  OP_LTE
META_PRANDOM  VALUE

And, if so, delete the expression statement and turn it into a
meta statement, where value expression is stashed inside the
meta statement struct.

We can do this because there is no 'set' support for prandom.
When printing a meta statement, check if the key is PRANDOM
and if so print the expression as a 'floating point' probability value.

Signed-off-by: Florian Westphal <fw@strlen.de>
---
 src/meta.c                | 16 ++++++++++++++++
 src/netlink_delinearize.c | 30 ++++++++++++++++++++++++++----
 2 files changed, 42 insertions(+), 4 deletions(-)

diff --git a/src/meta.c b/src/meta.c
index 2b0d5f0..bf8a430 100644
--- a/src/meta.c
+++ b/src/meta.c
@@ -601,8 +601,24 @@ struct expr *meta_expr_alloc(const struct location *loc, enum nft_meta_keys key)
 	return expr;
 }
 
+static void print_probability(const struct expr *expr)
+{
+	uint64_t value = mpz_get_uint64(expr->value);
+	double d, dividend;
+
+	dividend = (double)UINT_MAX;
+	d = (double)value;
+
+	printf("meta probability " META_PROB_FMT, d / dividend);
+}
+
 static void meta_stmt_print(const struct stmt *stmt)
 {
+	if (stmt->meta.key == NFT_META_PRANDOM) {
+		print_probability(stmt->meta.expr);
+		return;
+	}
+
 	if (meta_key_is_qualified(stmt->meta.key))
 		printf("meta %s set ", meta_templates[stmt->meta.key].token);
 	else
diff --git a/src/netlink_delinearize.c b/src/netlink_delinearize.c
index 7735699..138132a 100644
--- a/src/netlink_delinearize.c
+++ b/src/netlink_delinearize.c
@@ -1181,19 +1181,41 @@ static void ct_meta_common_postprocess(const struct expr *expr)
 static void meta_match_postprocess(struct rule_pp_ctx *ctx,
 				   const struct expr *expr)
 {
+	struct stmt *nstmt, *stmt = ctx->stmt;
+	struct expr *right = expr->right;
 	struct expr *left = expr->left;
 
 	switch (expr->op) {
+	case OP_LTE: {
+		uint64_t value;
+
+		if (stmt->expr != expr ||
+		    left->meta.key != NFT_META_PRANDOM ||
+		    right->ops->type != EXPR_VALUE)
+			break;
+
+		value = mpz_get_uint64(right->value);
+		if (value < 4 || value > UINT_MAX)
+			break;
+
+		nstmt = meta_stmt_alloc(&stmt->location, NFT_META_PRANDOM,
+					expr_get(expr->right));
+
+		list_add_tail(&nstmt->list, &stmt->list);
+		list_del(&stmt->list);
+		stmt_free(stmt);
+		ctx->stmt = nstmt;
+		break;
+	}
 	case OP_EQ:
-		if (expr->right->ops->type == EXPR_RANGE)
+		if (right->ops->type == EXPR_RANGE)
 			break;
 
-		expr->left->ops->pctx_update(&ctx->pctx, expr);
+		left->ops->pctx_update(&ctx->pctx, expr);
 
 		if (ctx->pdctx.pbase == PROTO_BASE_INVALID &&
 		    left->flags & EXPR_F_PROTOCOL)
-			payload_dependency_store(&ctx->pdctx, ctx->stmt,
-						 left->meta.base);
+			payload_dependency_store(&ctx->pdctx, stmt, left->meta.base);
 		break;
 	default:
 		ct_meta_common_postprocess(expr);
-- 
2.7.3


      parent reply	other threads:[~2016-07-05  7:35 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-07-05  7:35 [PATCH nft 1/3] meta: add random and probability match Florian Westphal
2016-07-05  7:35 ` [PATCH nft 1/3] meta: add random expression key Florian Westphal
2016-07-18 19:34   ` Pablo Neira Ayuso
2016-07-18 22:09     ` Florian Westphal
2016-07-05  7:35 ` [PATCH nft 2/3] meta: add short-hand mnemonic for probalistic matching Florian Westphal
2016-07-14 10:41   ` Pablo Neira Ayuso
2016-07-14 10:52     ` Florian Westphal
2016-07-14 11:32       ` Pablo Neira Ayuso
2016-07-14 12:08         ` Florian Westphal
2016-07-14 12:17           ` Pablo Neira Ayuso
2016-07-05  7:35 ` Florian Westphal [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1467704135-9154-4-git-send-email-fw@strlen.de \
    --to=fw@strlen.de \
    --cc=netfilter-devel@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).