Blame SOURCES/0052-JSON-Review-verdict-statement-and-expression.patch

bacbc8
From 9eb22ba5e195c97ca7a1e2baac7fec68b3f3b310 Mon Sep 17 00:00:00 2001
bacbc8
From: Phil Sutter <phil@nwl.cc>
bacbc8
Date: Wed, 29 Aug 2018 16:23:23 +0200
bacbc8
Subject: [PATCH] JSON: Review verdict statement and expression
bacbc8
bacbc8
Change jump and goto verdicts to become extensible by dedicating an
bacbc8
object for the target parameter.
bacbc8
bacbc8
While being at it, drop break and queue verdict expressions since they
bacbc8
don't seem to exist, no idea where I got those from in the first place.
bacbc8
For queue, there is a dedicated expression at least.
bacbc8
bacbc8
Signed-off-by: Phil Sutter <phil@nwl.cc>
bacbc8
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
bacbc8
(cherry picked from commit 8125785d5c5d35ec275e508166091d5472748bc1)
bacbc8
bacbc8
Conflicts:
bacbc8
	doc/libnftables-json.adoc
bacbc8
-> Dropped changes to non-existent libnftables JSON API documentation.
bacbc8
bacbc8
Signed-off-by: Phil Sutter <psutter@redhat.com>
bacbc8
---
bacbc8
 src/json.c           |  5 ++++-
bacbc8
 src/parser_json.c    | 10 ++++------
bacbc8
 tests/py/nft-test.py |  4 +++-
bacbc8
 3 files changed, 11 insertions(+), 8 deletions(-)
bacbc8
bacbc8
diff --git a/src/json.c b/src/json.c
bacbc8
index f74afd5a7292e..fad2a83624062 100644
bacbc8
--- a/src/json.c
bacbc8
+++ b/src/json.c
bacbc8
@@ -651,7 +651,10 @@ json_t *verdict_expr_json(const struct expr *expr, struct output_ctx *octx)
bacbc8
 		BUG("Unknown verdict %d.", expr->verdict);
bacbc8
 		return NULL;
bacbc8
 	}
bacbc8
-	return json_pack("{s:o}", name, chain ? json_string(chain) : json_null());
bacbc8
+	if (chain)
bacbc8
+		return json_pack("{s:{s:s}}", name, "target", chain);
bacbc8
+	else
bacbc8
+		return json_pack("{s:n}", name);
bacbc8
 }
bacbc8
 
bacbc8
 json_t *rt_expr_json(const struct expr *expr, struct output_ctx *octx)
bacbc8
diff --git a/src/parser_json.c b/src/parser_json.c
bacbc8
index 1e3688b2dc1cd..e3f20ae51c764 100644
bacbc8
--- a/src/parser_json.c
bacbc8
+++ b/src/parser_json.c
bacbc8
@@ -1008,27 +1008,25 @@ static struct expr *json_parse_verdict_expr(struct json_ctx *ctx,
bacbc8
 		bool chain;
bacbc8
 	} verdict_tbl[] = {
bacbc8
 		{ NFT_CONTINUE, "continue", false },
bacbc8
-		{ NFT_BREAK, "break", false },
bacbc8
 		{ NFT_JUMP, "jump", true },
bacbc8
 		{ NFT_GOTO, "goto", true },
bacbc8
 		{ NFT_RETURN, "return", false },
bacbc8
 		{ NF_ACCEPT, "accept", false },
bacbc8
 		{ NF_DROP, "drop", false },
bacbc8
-		{ NF_QUEUE, "queue", false },
bacbc8
 	};
bacbc8
 	const char *chain = NULL;
bacbc8
 	unsigned int i;
bacbc8
 
bacbc8
-	json_unpack(root, "s", &chain);
bacbc8
+	json_unpack(root, "{s:s}", "target", &chain);
bacbc8
 
bacbc8
 	for (i = 0; i < array_size(verdict_tbl); i++) {
bacbc8
 		if (strcmp(type, verdict_tbl[i].name))
bacbc8
 			continue;
bacbc8
 
bacbc8
-		if (verdict_tbl[i].chain && !chain) {
bacbc8
-			json_error(ctx, "Verdict %s needs chain argument.", type);
bacbc8
+		if (verdict_tbl[i].chain &&
bacbc8
+		    json_unpack_err(ctx, root, "{s:s}", "target", &chain))
bacbc8
 			return NULL;
bacbc8
-		}
bacbc8
+
bacbc8
 		return verdict_expr_alloc(int_loc,
bacbc8
 					  verdict_tbl[i].verdict, chain);
bacbc8
 	}
bacbc8
diff --git a/tests/py/nft-test.py b/tests/py/nft-test.py
bacbc8
index c02294ac54562..08569fbe0de44 100755
bacbc8
--- a/tests/py/nft-test.py
bacbc8
+++ b/tests/py/nft-test.py
bacbc8
@@ -277,7 +277,9 @@ def chain_create(chain, table, filename):
bacbc8
         print_error(reason, filename, chain.lineno)
bacbc8
         return -1
bacbc8
 
bacbc8
-    cmd = "add chain %s %s { %s; }" % (table, chain, chain.config)
bacbc8
+    cmd = "add chain %s %s" % (table, chain)
bacbc8
+    if chain.config:
bacbc8
+        cmd += " { %s; }" % chain.config
bacbc8
 
bacbc8
     ret = execute_cmd(cmd, filename, chain.lineno)
bacbc8
     if ret != 0:
bacbc8
-- 
bacbc8
2.21.0
bacbc8