Skip to contentSkip to conversation

flowtable json fixes

PATCH In review Florian Westphal 3 participants 10 messages Feb 7, 2022 netfilter

lore
Share & tools
lore

Florian Westphal

author maintainer
#0 lore raw

cover flowtable json fixes

json doesn't export the flow statement.
First patch resolves this, the other two patches resolve import problems with flowtable json format.

Florian Westphal (3):
json: add flow statement json export + parser
parser_json: fix flowtable device datatype
parser_json: permit empty device list

 include/json.h    |  2 ++
 src/ct.c          |  1 +
 src/json.c        |  7 ++++++
 src/parser_json.c | 54 ++++++++++++++++++++++++++++++++++++-----------
 4 files changed, 52 insertions(+), 12 deletions(-)
Signature · 1 line
2.34.1
author maintainer
#1 lore raw

PATCH 1/3 json: add flow statement json export + parser

flow statement has no export, its shown as:
".. }, "flow add @ft" ] } }"

With this patch:

".. }, {"flow": {"op": "add", "flowtable": "@ft"}}]}}"

---
 include/json.h    |  2 ++
 src/ct.c          |  1 +
 src/json.c        |  7 +++++++
 src/parser_json.c | 23 +++++++++++++++++++++++
 4 files changed, 33 insertions(+)
include/json.h +2 -0
index 3db9f2782d11..a753f359aa52 100644
@@ -69,6 +69,7 @@ json_t *uid_type_json(const struct expr *expr, struct output_ctx *octx);
 json_t *gid_type_json(const struct expr *expr, struct output_ctx *octx);
 
 json_t *expr_stmt_json(const struct stmt *stmt, struct output_ctx *octx);
+json_t *flow_offload_stmt_json(const struct stmt *stmt, struct output_ctx *octx);
 json_t *payload_stmt_json(const struct stmt *stmt, struct output_ctx *octx);
 json_t *exthdr_stmt_json(const struct stmt *stmt, struct output_ctx *octx);
 json_t *quota_stmt_json(const struct stmt *stmt, struct output_ctx *octx);
@@ -169,6 +170,7 @@ EXPR_PRINT_STUB(uid_type)
 EXPR_PRINT_STUB(gid_type)
 
 STMT_PRINT_STUB(expr)
+STMT_PRINT_STUB(flow_offload)
 STMT_PRINT_STUB(payload)
 STMT_PRINT_STUB(exthdr)
 STMT_PRINT_STUB(quota)
src/ct.c +1 -0
index 6049157198ad..e246d3039240 100644
@@ -578,6 +578,7 @@ static const struct stmt_ops flow_offload_stmt_ops = {
 	.name		= "flow_offload",
 	.print		= flow_offload_stmt_print,
 	.destroy	= flow_offload_stmt_destroy,
+	.json		= flow_offload_stmt_json,
 };
 
 struct stmt *flow_offload_stmt_alloc(const struct location *loc,
src/json.c +7 -0
index 63b325afc8d1..4f800c908c66 100644
@@ -1122,6 +1122,13 @@ json_t *expr_stmt_json(const struct stmt *stmt, struct output_ctx *octx)
 	return expr_print_json(stmt->expr, octx);
 }
 
+json_t *flow_offload_stmt_json(const struct stmt *stmt, struct output_ctx *octx)
+{
+	return json_pack("{s:{s:s, s:s+}}", "flow",
+			 "op", "add", "flowtable",
+			 "@", stmt->flow.table_name);
+}
+
 json_t *payload_stmt_json(const struct stmt *stmt, struct output_ctx *octx)
 {
 	return json_pack("{s: {s:o, s:o}}", "mangle",
src/parser_json.c +23 -0
index 2fad308f7783..f07b798adecd 100644
@@ -1903,6 +1903,28 @@ out_err:
 	return NULL;
 }
 
+static struct stmt *json_parse_flow_offload_stmt(struct json_ctx *ctx,
+						 const char *key, json_t *value)
+{
+	const char *opstr, *flowtable;
+
+	if (json_unpack_err(ctx, value, "{s:s, s:s}",
+			    "op", &opstr, "flowtable", &flowtable))
+		return NULL;
+
+	if (strcmp(opstr, "add")) {
+		json_error(ctx, "Unknown flow offload statement op '%s'.", opstr);
+		return NULL;
+	}
+
+	if (flowtable[0] != '@') {
+		json_error(ctx, "Illegal flowtable reference in flow offload statement.");
+		return NULL;
+	}
+
+	return flow_offload_stmt_alloc(int_loc, xstrdup(flowtable + 1));
+}
+
 static struct stmt *json_parse_notrack_stmt(struct json_ctx *ctx,
 					const char *key, json_t *value)
 {
@@ -2647,6 +2669,7 @@ static struct stmt *json_parse_stmt(struct json_ctx *ctx, json_t *root)
 		{ "mangle", json_parse_mangle_stmt },
 		{ "quota", json_parse_quota_stmt },
 		{ "limit", json_parse_limit_stmt },
+		{ "flow", json_parse_flow_offload_stmt },
 		{ "fwd", json_parse_fwd_stmt },
 		{ "notrack", json_parse_notrack_stmt },
 		{ "dup", json_parse_dup_stmt },
Signature · 1 line
2.34.1
author maintainer
#2 lore raw

Re: [PATCH nft 1/3] json: add flow statement json export + parser

Quoted 6 lines · Florian Westphal

flow statement has no export, its shown as:
".. }, "flow add @ft" ] } }"

With this patch:

".. }, {"flow": {"op": "add", "flowtable": "@ft"}}]}}"

This is based on the 'set' statement. If you prefer the @ to be removed let me know.

#3 lore raw

Re: [PATCH nft 1/3] json: add flow statement json export + parser

Quoted 10 lines · Florian Westphal
Quoted 6 lines · Florian Westphal

flow statement has no export, its shown as:
".. }, "flow add @ft" ] } }"

With this patch:

".. }, {"flow": {"op": "add", "flowtable": "@ft"}}]}}"

This is based on the 'set' statement. If you prefer the @ to be removed let me know.

Then, it is consistent with the existing syntax. So either we consider deprecating the @ on the 'set' statement (while retaining backward compatibility) or flowtable also includes it as in your patch.

reviewer
#4 lore raw

Re: [PATCH nft 1/3] json: add flow statement json export + parser

Hi,

Quoted 7 lines · Pablo Neira Ayuso
Quoted 6 lines · Florian Westphal
Quoted 5 lines · Florian Westphal

flow statement has no export, its shown as:
".. }, "flow add @ft" ] } }"

With this patch:

Quoted 8 lines
Quoted 4 lines
Quoted 1 line

".. }, {"flow": {"op": "add", "flowtable": "@ft"}}]}}"

This is based on the 'set' statement. If you prefer the @ to be removed let me know.

Then, it is consistent with the existing syntax. So either we consider deprecating the @ on the 'set' statement (while retaining backward compatibility) or flowtable also includes it as in your patch.

ACK, we should strive for internal consistency. I admittedly don't recall why I added the '@' prefix in output, parser even demands it. Dropping (i.e., omitting in output and accepting non-prefixed input) is fine with me!

Thanks, Phil

author maintainer
#5 lore raw

Re: [PATCH nft 1/3] json: add flow statement json export + parser

In reply to Pablo Neira Ayuso
Quoted 7 lines · Pablo Neira Ayuso
Quoted 6 lines · Florian Westphal
Quoted 5 lines · Florian Westphal

flow statement has no export, its shown as:
".. }, "flow add @ft" ] } }"

With this patch:

Quoted 8 lines
Quoted 4 lines
Quoted 1 line

".. }, {"flow": {"op": "add", "flowtable": "@ft"}}]}}"

This is based on the 'set' statement. If you prefer the @ to be removed let me know.

Then, it is consistent with the existing syntax. So either we consider deprecating the @ on the 'set' statement (while retaining backward compatibility) or flowtable also includes it as in your patch.

Ok, then lets keep it as-is; the @ might help humans to find a set/flowtable name more quickly this way.

#6 lore raw

Re: [PATCH nft 1/3] json: add flow statement json export + parser

Quoted 13 lines · Florian Westphal
Quoted 12 lines · Pablo Neira Ayuso
Quoted 10 lines · Florian Westphal
Quoted 6 lines · Florian Westphal

flow statement has no export, its shown as:
".. }, "flow add @ft" ] } }"

With this patch:

".. }, {"flow": {"op": "add", "flowtable": "@ft"}}]}}"

This is based on the 'set' statement. If you prefer the @ to be removed let me know.

Quoted 6 lines
Quoted 3 lines

Then, it is consistent with the existing syntax. So either we consider deprecating the @ on the 'set' statement (while retaining backward compatibility) or flowtable also includes it as in your patch.

Ok, then lets keep it as-is; the @ might help humans to find a set/flowtable name more quickly this way.

OK.

author maintainer
#7 lore raw

PATCH 2/3 parser_json: fix flowtable device datatype

In reply to Florian Westphal

Failed with: BUG: invalid expresion type symbol

Fixes 78bbe7f7a55be489 ("mnl: do not use expr->identifier to fetch device name")
---
 src/parser_json.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
src/parser_json.c +6 -2
index f07b798adecd..2ab0196461e2 100644
@@ -3125,7 +3125,9 @@ static struct expr *json_parse_flowtable_devs(struct json_ctx *ctx,
 	size_t index;
 
 	if (!json_unpack(root, "s", &dev)) {
-		tmp = symbol_expr_alloc(int_loc, SYMBOL_VALUE, NULL, dev);
+		tmp = constant_expr_alloc(int_loc, &string_type,
+					  BYTEORDER_HOST_ENDIAN,
+					  strlen(dev) * BITS_PER_BYTE, dev);
 		compound_expr_add(expr, tmp);
 		return expr;
 	}
@@ -3141,7 +3143,9 @@ static struct expr *json_parse_flowtable_devs(struct json_ctx *ctx,
 			expr_free(expr);
 			return NULL;
 		}
-		tmp = symbol_expr_alloc(int_loc, SYMBOL_VALUE, NULL, dev);
+		tmp = constant_expr_alloc(int_loc, &string_type,
+					  BYTEORDER_HOST_ENDIAN,
+					  strlen(dev) * BITS_PER_BYTE, dev);
 		compound_expr_add(expr, tmp);
 	}
 	return expr;
Signature · 1 line
2.34.1
author maintainer
#8 lore raw

PATCH 3/3 parser_json: permit empty device list

In reply to Florian Westphal

Normal input parser allows flowtables without 'devices' token, which makes the json export part elide 'dev' entirely, this then breaks on re-import:

$ nft -j -f json.dump
/tmp/json_1:1:14-14: Error: Object item not found: dev
---
 src/parser_json.c | 23 +++++++++++++----------
 1 file changed, 13 insertions(+), 10 deletions(-)
src/parser_json.c +13 -10
index 2ab0196461e2..4913260434f4 100644
@@ -3158,7 +3158,7 @@ static struct cmd *json_parse_cmd_add_flowtable(struct json_ctx *ctx,
 	const char *family, *hook, *hookstr;
 	struct flowtable *flowtable;
 	struct handle h = { 0 };
-	json_t *devs;
+	json_t *devs = NULL;
 	int prio;
 
 	if (json_unpack_err(ctx, root, "{s:s, s:s}",
@@ -3187,14 +3187,15 @@ static struct cmd *json_parse_cmd_add_flowtable(struct json_ctx *ctx,
 	if (op == CMD_DELETE || op == CMD_LIST)
 		return cmd_alloc(op, cmd_obj, &h, int_loc, NULL);
 
-	if (json_unpack_err(ctx, root, "{s:s, s:I, s:o}",
+	if (json_unpack_err(ctx, root, "{s:s, s:I}",
 			    "hook", &hook,
-			    "prio", &prio,
-			    "dev", &devs)) {
+			    "prio", &prio)) {
 		handle_free(&h);
 		return NULL;
 	}
 
+	json_unpack(root, "{s:o}", &devs);
+
 	hookstr = chain_hookname_lookup(hook);
 	if (!hookstr) {
 		json_error(ctx, "Invalid flowtable hook '%s'.", hook);
@@ -3209,12 +3210,14 @@ static struct cmd *json_parse_cmd_add_flowtable(struct json_ctx *ctx,
 				    BYTEORDER_HOST_ENDIAN,
 				    sizeof(int) * BITS_PER_BYTE, &prio);
 
-	flowtable->dev_expr = json_parse_flowtable_devs(ctx, devs);
-	if (!flowtable->dev_expr) {
-		json_error(ctx, "Invalid flowtable dev.");
-		flowtable_free(flowtable);
-		handle_free(&h);
-		return NULL;
+	if (devs) {
+		flowtable->dev_expr = json_parse_flowtable_devs(ctx, devs);
+		if (!flowtable->dev_expr) {
+			json_error(ctx, "Invalid flowtable dev.");
+			flowtable_free(flowtable);
+			handle_free(&h);
+			return NULL;
+		}
 	}
 	return cmd_alloc(op, cmd_obj, &h, int_loc, flowtable);
 }
Signature · 1 line
2.34.1
reviewer Acked-by on the v1 series
#9 lore raw
In reply to Florian Westphal
Quoted 8 lines · Florian Westphal

json doesn't export the flow statement.
First patch resolves this, the other two patches resolve import problems with flowtable json format.

Florian Westphal (3):
json: add flow statement json export + parser
parser_json: fix flowtable device datatype
parser_json: permit empty device list

Series:

Thanks, Phil