: parser cleanups
cover : parser cleanups
As preparation for my parser rearrangement, I've cleaned up the grammar a bit. The first two patches changes the queue statement to take an expression for the queue numbers, the second one cleans up the export format rules.
PATCH 1/3 expr: make range_low()/range_high() usable outside of segtree
Their functionality is also needed for set descriptions, move the functions to expressions.c and give them a more suitable name for global functions.
--- include/expression.h | 4 ++++ src/expression.c | 38 ++++++++++++++++++++++++++++++++++++++ src/segtree.c | 42 ++---------------------------------------- 3 files changed, 44 insertions(+), 40 deletions(-)
include/expression.h +4 -0
@@ -351,4 +351,8 @@ extern struct expr *map_expr_alloc(const struct location *loc,extern struct expr *set_ref_expr_alloc (const struct location *loc,struct set *set); + +extern void range_expr_value_low (mpz_t rop,const struct expr *expr); +extern void range_expr_value_high (mpz_t rop,const struct expr *expr); +#endif /* NFTABLES_EXPRESSION_H */
src/expression.c +38 -0
@@ -879,3 +879,41 @@ struct expr *set_ref_expr_alloc(const struct location *loc, struct set *set) expr->flags |=EXPR_F_CONSTANT ;return expr; } + +void range_expr_value_low (mpz_t rop,const struct expr *expr) +{ +switch (expr->ops->type) { +case EXPR_VALUE : +return mpz_set (rop, expr->value); +case EXPR_PREFIX : +return range_expr_value_low (rop, expr->prefix); +case EXPR_RANGE : +return range_expr_value_low (rop, expr->left); +case EXPR_MAPPING : +return range_expr_value_low (rop, expr->left); +default : +BUG ("invalid range expression type %s\n" , expr->ops->name); + } +} + +void range_expr_value_high (mpz_t rop,const struct expr *expr) +{ +mpz_t tmp; + +switch (expr->ops->type) { +case EXPR_VALUE : +return mpz_set (rop, expr->value); +case EXPR_PREFIX : +range_expr_value_low (rop, expr->prefix); +mpz_init_bitmask (tmp, expr->len - expr->prefix_len); +mpz_add (rop, rop, tmp); +mpz_clear (tmp); +return ; +case EXPR_RANGE : +return range_expr_value_high (rop, expr->right); +case EXPR_MAPPING : +return range_expr_value_high (rop, expr->left); +default : +BUG ("invalid range expression type %s\n" , expr->ops->name); + } +}
src/segtree.c +2 -40
54 lines not on this page · raw message
Signature · 1 line
1.9.3
PATCH 2/3 queue: clean up queue statement
In reply to Patrick McHardy- Rename keyword tokens to their actual keyword
- Change the grammar to follow the standard schema for statements and arguments
- Use actual expression for the queue numbers to support using normal range expressions, symbolic expression and so on.
- restore comma seperation of flag keywords
The result is that its possible to use standard ranges, prefix expressions, symbolic expressions etc for the queue number. We get checks for overflow, negative ranges and so on automatically.
The comma seperation of flags is more similar to what we have for other flag values. It is still possible to use spaces, however this could be removed since we never had a release supporting that.
--- include/statement.h | 3 +-- src/evaluate.c | 15 ++++++++++- src/netlink_delinearize.c | 21 +++++++++++----- src/netlink_linearize.c | 22 +++++++++++------ src/parser.y | 63 +++++++++++++---------------------------------- src/scanner.l | 4 +-- src/statement.c | 19 ++++++++------ 7 files changed, 75 insertions(+), 72 deletions(-)
include/statement.h +1 -2
@@ -70,8 +70,7 @@ struct nat_stmt {extern struct stmt *nat_stmt_alloc (const struct location *loc);struct queue_stmt { -uint16_t from; -uint16_t to; +struct expr *queue;uint16_t flags; };
src/evaluate.c +14 -1
@@ -1180,6 +1180,19 @@ static int stmt_evaluate_ct(struct eval_ctx *ctx, struct stmt *stmt)return 0 ; } +static int stmt_evaluate_queue (struct eval_ctx *ctx,struct stmt *stmt) +{ +if (stmt->queue.queue !=NULL ) { +expr_set_context (&ctx->ectx, &integer_type,16 ); +if (expr_evaluate (ctx, &stmt->queue.queue) <0 ) +return -1 ; +if (!expr_is_constant (stmt->queue.queue)) +return expr_error (ctx->msgs, stmt->queue.queue, +"queue number is not constant" ); + } +return 0 ; +} +static int stmt_evaluate_log (struct eval_ctx *ctx,struct stmt *stmt) { if (stmt->log.flags & STMT_LOG_LEVEL &&
@@ -1219,7 +1232,7 @@ static int stmt_evaluate(struct eval_ctx *ctx, struct stmt *stmt)case STMT_NAT :return stmt_evaluate_nat (ctx, stmt);case STMT_QUEUE : -return 0 ; +return stmt_evaluate_queue (ctx, stmt);case STMT_CT :return stmt_evaluate_ct (ctx, stmt);default :
src/netlink_delinearize.c +15 -6
@@ -558,15 +558,24 @@ static void netlink_parse_queue(struct netlink_parse_ctx *ctx,const struct location *loc, const struct nft_rule_expr *nle) { + struct expr *expr, *high;struct stmt *stmt; -uint16_t range_to; +uint16_t num, total; + + num =nft_rule_expr_get_u16 (nle,NFT_EXPR_QUEUE_NUM ); + total =nft_rule_expr_get_u16 (nle,NFT_EXPR_QUEUE_TOTAL ); + + expr =constant_expr_alloc (loc, &integer_type, +BYTEORDER_HOST_ENDIAN ,16 , &num); +if (total >1 ) { + total += num -1 ; + high =constant_expr_alloc (loc, &integer_type, +BYTEORDER_HOST_ENDIAN ,16 , &total); + expr =range_expr_alloc (loc, expr, high); + } stmt =queue_stmt_alloc (loc); - stmt->queue.from =nft_rule_expr_get_u16 (nle,NFT_EXPR_QUEUE_NUM ); - range_to =nft_rule_expr_get_u16 (nle,NFT_EXPR_QUEUE_TOTAL ); - range_to += stmt->queue.from -1 ; - stmt->queue.to = range_to; - + stmt->queue.queue = expr; stmt->queue.flags =nft_rule_expr_get_u16 (nle,NFT_EXPR_QUEUE_FLAGS );list_add_tail (&stmt->list, &ctx->rule->stmts); }
src/netlink_linearize.c +14 -8
@@ -687,21 +687,27 @@ static void netlink_gen_queue_stmt(struct netlink_linearize_ctx *ctx, {struct nft_rule_expr *nle;uint16_t total_queues; +mpz_t low, high; - nle =alloc_nft_expr ("queue" ); - -nft_rule_expr_set_u16 (nle,NFT_EXPR_QUEUE_NUM , - stmt->queue.from); - - total_queues = stmt->queue.to - stmt->queue.from; -nft_rule_expr_set_u16 (nle,NFT_EXPR_QUEUE_TOTAL , - total_queues +1 ); +mpz_init2 (low,16 ); +mpz_init2 (high,16 ); +if (stmt->queue.queue !=NULL ) { +range_expr_value_low (low, stmt->queue.queue); +range_expr_value_high (high, stmt->queue.queue); + } + total_queues =mpz_get_uint16 (high) -mpz_get_uint16 (low) +1 ; + nle =alloc_nft_expr ("queue" ); +nft_rule_expr_set_u16 (nle,NFT_EXPR_QUEUE_NUM ,mpz_get_uint16 (low)); +nft_rule_expr_set_u16 (nle,NFT_EXPR_QUEUE_TOTAL , total_queues);if (stmt->queue.flags) {nft_rule_expr_set_u16 (nle,NFT_EXPR_QUEUE_FLAGS , stmt->queue.flags); }nft_rule_add_expr (ctx->nlr, nle); + +mpz_clear (low); +mpz_clear (high); } static void netlink_gen_ct_stmt(struct netlink_linearize_ctx *ctx,
src/parser.y +17 -46
102 lines not on this page · raw message
src/scanner.l +2 -2
@@ -292,8 +292,8 @@ addrstring ({macaddr}|{ip4addr}|{ip6addr}) "queue" { return QUEUE;} "num" { return QUEUENUM;} -"bypass" { return QUEUEBYPASS;} -"fanout" { return QUEUECPUFANOUT;} +"bypass" { return BYPASS;} +"fanout" { return FANOUT;} "limit" { return LIMIT; } "rate" { return RATE; }
src/statement.c +12 -7
@@ -197,14 +197,19 @@ struct stmt *limit_stmt_alloc(const struct location *loc)static void queue_stmt_print (const struct stmt *stmt) { -printf ("queue num %u" , - stmt->queue.from); -if (stmt->queue.to && stmt->queue.to != stmt->queue.from) -printf ("-%u" , stmt->queue.to); -if (stmt->queue.flags &NFT_QUEUE_FLAG_BYPASS ) -printf (" bypass" ); +const char *delim =" " ; + +printf ("queue" ); +if (stmt->queue.queue !=NULL ) { +printf (" num " ); +expr_print (stmt->queue.queue); + } +if (stmt->queue.flags &NFT_QUEUE_FLAG_BYPASS ) { +printf ("%sbypass" , delim); + delim ="," ; + }if (stmt->queue.flags &NFT_QUEUE_FLAG_CPU_FANOUT ) -printf (" fanout" ); +printf ("%sfanout" , delim); }
Signature · 1 line
1.9.3
PATCH 3/3 parser: rearrange monitor/export rules
In reply to Patrick McHardyMove the output format rules next to the monitor and export command rules, format them similar to other simple value mappings and unify their naming.
--- src/parser.y | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-)
src/parser.y +11 -12
@@ -513,7 +513,8 @@ static void location_update(struct location *loc, struct location *rhs, int n) %destructor { expr_free($$); } ct_expr %type <val> ct_key -%type <val> export_format output_format monitor_event monitor_object +%type <val> export_format +%type <val> monitor_event monitor_object monitor_format %%
@@ -780,7 +781,7 @@ export_cmd : export_format } ; -monitor_cmd : monitor_event monitor_object output_format +monitor_cmd : monitor_event monitor_object monitor_format { struct handle h = { .family = NFPROTO_UNSPEC }; $$ = cmd_alloc(CMD_MONITOR, CMD_OBJ_RULESET, &h, &@$, NULL);
@@ -860,6 +861,14 @@ monitor_object : /* empty */ } ; +monitor_format : /* empty */ { $$ = NFT_OUTPUT_DEFAULT; } + | export_format + ; + +export_format : XML { $$ = NFT_OUTPUT_XML; } + | JSON { $$ = NFT_OUTPUT_JSON; } + ; + describe_cmd : primary_expr { struct handle h = { .family = NFPROTO_UNSPEC };
@@ -868,13 +877,6 @@ describe_cmd : primary_expr } ; -output_format : /* empty */ - { - $$ = NFT_OUTPUT_DEFAULT; - } - | export_format - ; - table_block_alloc : /* empty */ { $$ = table_alloc();
@@ -2168,7 +2170,4 @@ mh_hdr_field : NEXTHDR { $$ = MHHDR_NEXTHDR; } | CHECKSUM { $$ = MHHDR_CHECKSUM; } ; -export_format : XML { $$ = NFT_OUTPUT_XML; } - | JSON { $$ = NFT_OUTPUT_JSON; } - ; %%
Signature · 1 line
1.9.3