flowtable json fixes
Florian Westphal
maintainercover 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
Florian Westphal
maintainerPATCH 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
@@ -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
@@ -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
@@ -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
@@ -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
Florian Westphal
maintainerRe: [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.
Pablo Neira Ayuso
maintainerRe: [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.
Phil Sutter
reviewerRe: [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
Florian Westphal
maintainerRe: [PATCH nft 1/3] json: add flow statement json export + parser
In reply to Pablo Neira AyusoQuoted 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.
Pablo Neira Ayuso
maintainerRe: [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.
Florian Westphal
maintainerPATCH 2/3 parser_json: fix flowtable device datatype
In reply to Florian WestphalFailed with: BUG: invalid expresion type symbol
--- src/parser_json.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)
src/parser_json.c +6 -2
@@ -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
Florian Westphal
maintainerPATCH 3/3 parser_json: permit empty device list
In reply to Florian WestphalNormal 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
@@ -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
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