src: support for flowtable listing
Pablo Neira Ayuso
maintainerPATCH 1/6 src: support for flowtable listing
This patch allows you to dump existing flowtable.
# nft list ruleset
table ip x {
flowtable x {
hook ingress priority 10
devices = { eth0, tap0 }
}
}You can also list existing flowtables via:
# nft list flowtables
table ip x {
flowtable x {
hook ingress priority 10
devices = { eth0, tap0 }
}
}You need a Linux kernel >= 4.16-rc to test this new feature.
--- include/linux/netfilter/nf_tables.h | 6 ++ include/mnl.h | 3 + include/netlink.h | 4 ++ include/rule.h | 24 +++++++ src/evaluate.c | 1 + src/mnl.c | 58 +++++++++++++++++ src/netlink.c | 65 +++++++++++++++++++ src/parser_bison.y | 6 ++ src/rule.c | 122 ++++++++++++++++++++++++++++++++++++ src/scanner.l | 2 + 10 files changed, 291 insertions(+)
include/linux/netfilter/nf_tables.h +6 -0
@@ -92,6 +92,9 @@ enum nft_verdicts {* @NFT_MSG_GETOBJ: get a stateful object (enum nft_obj_attributes) * @NFT_MSG_DELOBJ: delete a stateful object (enum nft_obj_attributes) * @NFT_MSG_GETOBJ_RESET: get and reset a stateful object (enum nft_obj_attributes) +* @NFT_MSG_NEWFLOWTABLE: add new flow table (enum nft_flowtable_attributes) +* @NFT_MSG_GETFLOWTABLE: get flow table (enum nft_flowtable_attributes) +* @NFT_MSG_DELFLOWTABLE: delete flow table (enum nft_flowtable_attributes) */ enum nf_tables_msg_types {NFT_MSG_NEWTABLE ,
@@ -116,6 +119,9 @@ enum nf_tables_msg_types {NFT_MSG_GETOBJ ,NFT_MSG_DELOBJ ,NFT_MSG_GETOBJ_RESET , +NFT_MSG_NEWFLOWTABLE , +NFT_MSG_GETFLOWTABLE , +NFT_MSG_DELFLOWTABLE , NFT_MSG_MAX, };
include/mnl.h +3 -0
@@ -89,6 +89,9 @@ int mnl_nft_obj_batch_add(struct nftnl_obj *nln, struct nftnl_batch *batch,int mnl_nft_obj_batch_del (struct nftnl_obj *nln,struct nftnl_batch *batch,unsigned int flags,uint32_t seqnum); +struct nftnl_flowtable_list * +mnl_nft_flowtable_dump (struct netlink_ctx *ctx,int family,const char *table); +struct nftnl_ruleset *mnl_nft_ruleset_dump (struct netlink_ctx *ctx,uint32_t family); int mnl_nft_event_listener(struct mnl_socket *nf_sock, unsigned int debug_mask,
include/netlink.h +4 -0
@@ -181,6 +181,10 @@ extern int netlink_add_obj(struct netlink_ctx *ctx, const struct handle *h,extern int netlink_delete_obj (struct netlink_ctx *ctx,const struct handle *h,struct location *loc,uint32_t type); +extern int netlink_list_flowtables (struct netlink_ctx *ctx, +const struct handle *h, +const struct location *loc); +extern void netlink_dump_chain (const struct nftnl_chain *nlc,struct netlink_ctx *ctx); extern void netlink_dump_rule(const struct nftnl_rule *nlr,
include/rule.h +24 -0
@@ -35,6 +35,7 @@ struct position_spec {* @chain: chain name (chains and rules only) * @set: set name (sets only) * @obj: stateful object name (stateful object only) +* @flowtable: flow table name (flow table only) * @handle: rule handle (rules only) * @position: rule position (rules only) * @set_id: set ID (sets only)
@@ -45,6 +46,7 @@ struct handle {const char *chain;const char *set;const char *obj; +const char *flowtable;struct handle_spec handle;struct position_spec position;uint32_t set_id;
@@ -98,6 +100,7 @@ enum table_flags {* @chains: chains contained in the table * @sets: sets contained in the table * @objs: stateful objects contained in the table +* @flowtables: flow tables contained in the table * @flags: table flags * @refcnt: table reference counter */
@@ -109,6 +112,7 @@ struct table {struct list_head chains;struct list_head sets;struct list_head objs; +struct list_head flowtables;enum table_flags flags;unsigned int refcnt; };
@@ -313,6 +317,24 @@ void obj_print_plain(const struct obj *obj, struct output_ctx *octx);const char *obj_type_name (uint32_t type);uint32_t obj_type_to_cmd (uint32_t type); +struct flowtable { +struct list_head list; +struct handle handle; +struct location location; +unsigned int hooknum; +int priority; +const char **dev_array; +int dev_array_len; +unsigned int refcnt; +}; + +extern struct flowtable *flowtable_alloc (const struct location *loc); +extern struct flowtable *flowtable_get (struct flowtable *flowtable); +extern void flowtable_free (struct flowtable *flowtable); +extern void flowtable_add_hash (struct flowtable *flowtable,struct table *table); + +void flowtable_print (const struct flowtable *n,struct output_ctx *octx); +/** * enum cmd_ops - command operations *
@@ -371,6 +393,7 @@ enum cmd_ops {* @CMD_OBJ_QUOTAS: multiple quotas * @CMD_OBJ_LIMIT: limit * @CMD_OBJ_LIMITS: multiple limits +* @CMD_OBJ_FLOWTABLES: flow tables */ enum cmd_obj {CMD_OBJ_INVALID ,
@@ -397,6 +420,7 @@ enum cmd_obj {CMD_OBJ_CT_HELPERS ,CMD_OBJ_LIMIT ,CMD_OBJ_LIMITS , + CMD_OBJ_FLOWTABLES, }; struct markup {
src/evaluate.c +1 -0
@@ -3193,6 +3193,7 @@ static int cmd_evaluate_list(struct eval_ctx *ctx, struct cmd *cmd)case CMD_OBJ_CT_HELPERS :case CMD_OBJ_LIMITS :case CMD_OBJ_SETS : +case CMD_OBJ_FLOWTABLES :if (cmd->handle.table ==NULL )return 0 ;if (table_lookup (&cmd->handle, ctx->cache) ==NULL )
src/mnl.c +58 -0
70 lines not on this page · raw message
src/netlink.c +65 -0
77 lines not on this page · raw message
src/parser_bison.y +6 -0
@@ -247,6 +247,8 @@ int nft_lex(void *, void *, void *); %token METER "meter" %token METERS "meters" +%token FLOWTABLES "flowtables" + %token <val> NUM "number" %token <string> STRING "string" %token <string> QUOTED_STRING "quoted string"
@@ -1103,6 +1105,10 @@ list_cmd : TABLE table_spec { $$ = cmd_alloc(CMD_LIST, CMD_OBJ_METER, &$2, &@$, NULL); } + | FLOWTABLES ruleset_spec + { + $$ = cmd_alloc(CMD_LIST, CMD_OBJ_FLOWTABLES, &$2, &@$, NULL); + } | MAPS ruleset_spec { $$ = cmd_alloc(CMD_LIST, CMD_OBJ_MAPS, &$2, &@$, NULL);
src/rule.c +122 -0
158 lines not on this page · raw message
src/scanner.l +2 -0
@@ -296,6 +296,8 @@ addrstring ({macaddr}|{ip4addr}|{ip6addr}) "meter" { return METER; } "meters" { return METERS; } +"flowtables" { return FLOWTABLES; } + "counter" { return COUNTER; } "name" { return NAME; } "packets" { return PACKETS; }
Signature · 1 line
2.11.0
Pablo Neira Ayuso
maintainerPATCH 2/6 src: add support to add flowtables
This patch allows you to create flowtable:
# nft add table x
# nft add flowtable x m { hook ingress priority 10\; devices = { eth0, wlan0 }\; }You have to specify hook and priority. So far, only the ingress hook is supported. The priority represents where this flowtable is placed in the ingress hook, which is registered to the devices that the user specifies.
You can also use the 'create' command instead to bail out in case that there is an existing flowtable with this name.
--- include/expression.h | 2 + include/mnl.h | 4 ++ include/netlink.h | 4 ++ include/rule.h | 7 ++++ src/evaluate.c | 26 ++++++++++++ src/expression.c | 4 +- src/mnl.c | 16 ++++++++ src/netlink.c | 58 ++++++++++++++++++++++++++ src/parser_bison.y | 112 ++++++++++++++++++++++++++++++++++++++++++++++++--- src/rule.c | 17 ++++++++ src/scanner.l | 2 + 11 files changed, 245 insertions(+), 7 deletions(-)
include/expression.h +2 -0
@@ -407,6 +407,8 @@ extern struct expr *prefix_expr_alloc(const struct location *loc,extern struct expr *range_expr_alloc (const struct location *loc,struct expr *low,struct expr *high); +extern struct expr *compound_expr_alloc (const struct location *loc, +const struct expr_ops *ops);extern void compound_expr_add (struct expr *compound,struct expr *expr);extern void compound_expr_remove (struct expr *compound,struct expr *expr);extern void list_expr_sort (struct list_head *head);
include/mnl.h +4 -0
@@ -92,6 +92,10 @@ int mnl_nft_obj_batch_del(struct nftnl_obj *nln, struct nftnl_batch *batch,struct nftnl_flowtable_list *mnl_nft_flowtable_dump (struct netlink_ctx *ctx,int family,const char *table); +int mnl_nft_flowtable_batch_add (struct nftnl_flowtable *flo, +struct nftnl_batch *batch,unsigned int flags, +uint32_t seqnum); +struct nftnl_ruleset *mnl_nft_ruleset_dump (struct netlink_ctx *ctx,uint32_t family); int mnl_nft_event_listener(struct mnl_socket *nf_sock, unsigned int debug_mask,
include/netlink.h +4 -0
@@ -7,6 +7,7 @@#include <libnftnl/expr.h> #include <libnftnl/set.h> #include <libnftnl/object.h> +#include <libnftnl/flowtable.h> #include <linux/netlink.h> #include <linux/netfilter/nf_tables.h>
@@ -184,6 +185,9 @@ extern int netlink_delete_obj(struct netlink_ctx *ctx, const struct handle *h,extern int netlink_list_flowtables (struct netlink_ctx *ctx,const struct handle *h,const struct location *loc); +extern int netlink_add_flowtable (struct netlink_ctx *ctx, +const struct handle *h,struct flowtable *ft, +uint32_t flags);extern void netlink_dump_chain (const struct nftnl_chain *nlc,struct netlink_ctx *ctx);
include/rule.h +7 -0
@@ -320,10 +320,13 @@ uint32_t obj_type_to_cmd(uint32_t type);struct flowtable {struct list_head list;struct handle handle; +struct scope scope;struct location location; +const char * hookstr;unsigned int hooknum;int priority;const char **dev_array; +struct expr *dev_expr;int dev_array_len;unsigned int refcnt; };
@@ -381,6 +384,8 @@ enum cmd_ops {* @CMD_OBJ_CHAIN: chain * @CMD_OBJ_CHAINS: multiple chains * @CMD_OBJ_TABLE: table +* @CMD_OBJ_FLOWTABLE: flowtable +* @CMD_OBJ_FLOWTABLES: flowtables * @CMD_OBJ_RULESET: ruleset * @CMD_OBJ_EXPR: expression * @CMD_OBJ_MONITOR: monitor
@@ -420,6 +425,7 @@ enum cmd_obj {CMD_OBJ_CT_HELPERS ,CMD_OBJ_LIMIT ,CMD_OBJ_LIMITS , +CMD_OBJ_FLOWTABLE , CMD_OBJ_FLOWTABLES, };
@@ -479,6 +485,7 @@ struct cmd {struct rule *rule;struct chain *chain;struct table *table; +struct flowtable *flowtable;struct monitor *monitor;struct markup *markup;struct obj *object;
src/evaluate.c +26 -0
@@ -2894,6 +2894,24 @@ static int set_evaluate(struct eval_ctx *ctx, struct set *set)return 0 ; } +static uint32_t str2hooknum (uint32_t family,const char *hook); + +static int flowtable_evaluate (struct eval_ctx *ctx,struct flowtable *ft) +{ +struct table *table; + + table =table_lookup_global (ctx); +if (table ==NULL ) +return cmd_error (ctx,"Could not process rule: Table '%s' does not exist" , + ctx->cmd->handle.table); + + ft->hooknum =str2hooknum (NFPROTO_NETDEV , ft->hookstr); +if (ft->hooknum ==NF_INET_NUMHOOKS ) +return chain_error (ctx, ft,"invalid hook %s" , ft->hookstr); + +return 0 ; +} +static int rule_evaluate (struct eval_ctx *ctx,struct rule *rule) {struct stmt *stmt, *tstmt =NULL ;
@@ -3066,6 +3084,14 @@ static int cmd_evaluate_add(struct eval_ctx *ctx, struct cmd *cmd)return chain_evaluate (ctx, cmd->chain);case CMD_OBJ_TABLE :return table_evaluate (ctx, cmd->table); +case CMD_OBJ_FLOWTABLE : + ret =cache_update (ctx->nf_sock, ctx->cache, cmd->op, + ctx->msgs, ctx->debug_mask &NFT_DEBUG_NETLINK , ctx->octx); +if (ret <0 ) +return ret; + +handle_merge (&cmd->flowtable->handle, &cmd->handle); +return flowtable_evaluate (ctx, cmd->flowtable);case CMD_OBJ_COUNTER :case CMD_OBJ_QUOTA :case CMD_OBJ_CT_HELPER :
src/expression.c +2 -2
@@ -663,8 +663,8 @@ struct expr *range_expr_alloc(const struct location *loc,return expr; } -static struct expr *compound_expr_alloc (const struct location *loc, -const struct expr_ops *ops) +struct expr *compound_expr_alloc (const struct location *loc, +const struct expr_ops *ops) {struct expr *expr;
src/mnl.c +16 -0
@@ -1011,6 +1011,22 @@ err:return NULL ; } +int mnl_nft_flowtable_batch_add (struct nftnl_flowtable *flo, +struct nftnl_batch *batch,unsigned int flags, +uint32_t seqnum) +{ +struct nlmsghdr *nlh; + + nlh =nftnl_nlmsg_build_hdr (nftnl_batch_buffer (batch), +NFT_MSG_NEWFLOWTABLE , +nftnl_flowtable_get_u32 (flo,NFTNL_FLOWTABLE_FAMILY ), +NLM_F_CREATE | flags, seqnum); +nftnl_flowtable_nlmsg_build_payload (nlh, flo); +mnl_nft_batch_continue (batch); + +return 0 ; +} +/* * ruleset */
src/netlink.c +58 -0
64 lines not on this page · raw message
src/parser_bison.y +107 -5
178 lines not on this page · raw message
src/rule.c +17 -0
@@ -45,6 +45,8 @@ void handle_merge(struct handle *dst, const struct handle *src) dst->chain =xstrdup (src->chain);if (dst->set ==NULL && src->set !=NULL ) dst->set =xstrdup (src->set); +if (dst->flowtable ==NULL && src->flowtable !=NULL ) + dst->flowtable =xstrdup (src->flowtable);if (dst->obj ==NULL && src->obj !=NULL ) dst->obj =xstrdup (src->obj);if (dst->handle.id ==0 )
@@ -854,6 +856,7 @@ struct cmd *cmd_alloc(enum cmd_ops op, enum cmd_obj obj,void nft_cmd_expand (struct cmd *cmd) {struct list_head new_cmds; +struct flowtable *ft;struct table *table;struct chain *chain;struct rule *rule;
@@ -893,6 +896,14 @@ void nft_cmd_expand(struct cmd *cmd) &set->location, set_get(set));list_add_tail (&new->list, &new_cmds); } +list_for_each_entry (ft, &table->flowtables, list) { +handle_merge (&ft->handle, &table->handle); +memset (&h,0 ,sizeof (h)); +handle_merge (&h, &ft->handle); + new =cmd_alloc (CMD_ADD ,CMD_OBJ_FLOWTABLE , &h, + &ft->location,flowtable_get (ft)); +list_add_tail (&new->list, &new_cmds); + }list_for_each_entry (chain, &table->chains, list) {list_for_each_entry (rule, &chain->rules, list) {memset (&h,0 ,sizeof (h));
@@ -979,6 +990,9 @@ void cmd_free(struct cmd *cmd)case CMD_OBJ_LIMIT :obj_free (cmd->object);break ; +case CMD_OBJ_FLOWTABLE : +flowtable_free (cmd->flowtable); +break ;default :BUG ("invalid command object type %u\n" , cmd->obj); }
@@ -1068,6 +1082,9 @@ static int do_command_add(struct netlink_ctx *ctx, struct cmd *cmd, bool excl)case CMD_OBJ_CT_HELPER :case CMD_OBJ_LIMIT :return netlink_add_obj (ctx, &cmd->handle, cmd->object, flags); +case CMD_OBJ_FLOWTABLE : +return netlink_add_flowtable (ctx, &cmd->handle, cmd->flowtable, + flags);default :BUG ("invalid command object type %u\n" , cmd->obj); }
src/scanner.l +2 -0
@@ -238,6 +238,7 @@ addrstring ({macaddr}|{ip4addr}|{ip6addr}) "hook" { return HOOK; } "device" { return DEVICE; } +"devices" { return DEVICES; } "table" { return TABLE; } "tables" { return TABLES; } "chain" { return CHAIN; }
@@ -249,6 +250,7 @@ addrstring ({macaddr}|{ip4addr}|{ip6addr}) "element" { return ELEMENT; } "map" { return MAP; } "maps" { return MAPS; } +"flowtable" { return FLOWTABLE; } "handle" { return HANDLE; } "ruleset" { return RULESET; } "trace" { return TRACE; }
Signature · 1 line
2.11.0
Pablo Neira Ayuso
maintainerPATCH 3/6 src: delete flowtable
In reply to Pablo Neira AyusoThis patch allows you to delete an existing flowtable:
# nft delete flowtable x m
--- include/mnl.h | 3 +++ include/netlink.h | 3 +++ src/evaluate.c | 1 + src/mnl.c | 16 ++++++++++++++++ src/netlink.c | 18 ++++++++++++++++++ src/parser_bison.y | 4 ++++ src/rule.c | 3 +++ 7 files changed, 48 insertions(+)
include/mnl.h +3 -0
@@ -95,6 +95,9 @@ mnl_nft_flowtable_dump(struct netlink_ctx *ctx, int family, const char *table);int mnl_nft_flowtable_batch_add (struct nftnl_flowtable *flo,struct nftnl_batch *batch,unsigned int flags,uint32_t seqnum); +int mnl_nft_flowtable_batch_del (struct nftnl_flowtable *flow, +struct nftnl_batch *batch,unsigned int flags, +uint32_t seqnum);struct nftnl_ruleset *mnl_nft_ruleset_dump (struct netlink_ctx *ctx,uint32_t family);
include/netlink.h +3 -0
@@ -188,6 +188,9 @@ extern int netlink_list_flowtables(struct netlink_ctx *ctx,extern int netlink_add_flowtable (struct netlink_ctx *ctx,const struct handle *h,struct flowtable *ft,uint32_t flags); +extern int netlink_delete_flowtable (struct netlink_ctx *ctx, +const struct handle *h, +struct location *loc);extern void netlink_dump_chain (const struct nftnl_chain *nlc,struct netlink_ctx *ctx);
src/evaluate.c +1 -0
@@ -3118,6 +3118,7 @@ static int cmd_evaluate_delete(struct eval_ctx *ctx, struct cmd *cmd)case CMD_OBJ_RULE :case CMD_OBJ_CHAIN :case CMD_OBJ_TABLE : +case CMD_OBJ_FLOWTABLE :case CMD_OBJ_COUNTER :case CMD_OBJ_QUOTA :case CMD_OBJ_CT_HELPER :
src/mnl.c +16 -0
@@ -1027,6 +1027,22 @@ int mnl_nft_flowtable_batch_add(struct nftnl_flowtable *flo,return 0 ; } +int mnl_nft_flowtable_batch_del (struct nftnl_flowtable *flo, +struct nftnl_batch *batch,unsigned int flags, +uint32_t seqnum) +{ +struct nlmsghdr *nlh; + + nlh =nftnl_nlmsg_build_hdr (nftnl_batch_buffer (batch), +NFT_MSG_DELFLOWTABLE , +nftnl_flowtable_get_u32 (flo,NFTNL_FLOWTABLE_FAMILY ), + flags, seqnum); +nftnl_flowtable_nlmsg_build_payload (nlh, flo); +mnl_nft_batch_continue (batch); + +return 0 ; +} +/* * ruleset */
src/netlink.c +18 -0
@@ -1820,6 +1820,24 @@ int netlink_add_flowtable(struct netlink_ctx *ctx, const struct handle *h,return err; } +int netlink_delete_flowtable (struct netlink_ctx *ctx,const struct handle *h, +struct location *loc) +{ +struct nftnl_flowtable *flo; +int err; + + flo =alloc_nftnl_flowtable (h,NULL ); +netlink_dump_flowtable (flo, ctx); + + err =mnl_nft_flowtable_batch_del (flo, ctx->batch,0 , ctx->seqnum); +if (err <0 ) +netlink_io_error (ctx, loc,"Could not delete flowtable: %s" , +strerror (errno)); +nftnl_flowtable_free (flo); + +return err; +} +static int list_obj_cb (struct nftnl_obj *nls,void *arg) {struct netlink_ctx *ctx = arg;
src/parser_bison.y +4 -0
@@ -1023,6 +1023,10 @@ delete_cmd : TABLE table_spec { $$ = cmd_alloc(CMD_DELETE, CMD_OBJ_SETELEM, &$2, &@$, $3); } + | FLOWTABLE flowtable_spec + { + $$ = cmd_alloc(CMD_DELETE, CMD_OBJ_FLOWTABLE, &$2, &@$, NULL); + } | COUNTER obj_spec { $$ = cmd_alloc(CMD_DELETE, CMD_OBJ_COUNTER, &$2, &@$, NULL);
src/rule.c +3 -0
@@ -1174,6 +1174,9 @@ static int do_command_delete(struct netlink_ctx *ctx, struct cmd *cmd)case CMD_OBJ_LIMIT :return netlink_delete_obj (ctx, &cmd->handle, &cmd->location,NFT_OBJECT_LIMIT ); +case CMD_OBJ_FLOWTABLE : +return netlink_delete_flowtable (ctx, &cmd->handle, + &cmd->location);default :BUG ("invalid command object type %u\n" , cmd->obj); }
Signature · 1 line
2.11.0
Pablo Neira Ayuso
maintainerPATCH 4/6 src: flow offload support
In reply to Pablo Neira AyusoThis patch allows us to refer to existing flowtables:
# nft add rule x x flow offload @m
Packets matching this rule create an entry in the flow table 'm', hence, follow up packets that get to the flowtable at ingress bypass the classic forwarding path.
--- include/ct.h | 2 ++ include/statement.h | 9 +++++++++ src/ct.c | 23 +++++++++++++++++++++++ src/evaluate.c | 1 + src/netlink_delinearize.c | 11 +++++++++++ src/netlink_linearize.c | 13 +++++++++++++ src/parser_bison.y | 5 +++++ src/scanner.l | 1 + 8 files changed, 65 insertions(+)
include/ct.h +2 -0
@@ -29,6 +29,8 @@ extern struct expr *ct_expr_alloc(const struct location *loc,extern void ct_expr_update_type (struct proto_ctx *ctx,struct expr *expr);extern struct stmt *notrack_stmt_alloc (const struct location *loc); +extern struct stmt *flow_offload_stmt_alloc (const struct location *loc, +const char *table_name);extern const struct datatype ct_dir_type;extern const struct datatype ct_state_type;
include/statement.h +9 -0
@@ -10,6 +10,12 @@ extern struct stmt *expr_stmt_alloc(const struct location *loc,extern struct stmt *verdict_stmt_alloc (const struct location *loc,struct expr *expr); +struct flow_stmt { +const char *table_name; +}; + +struct stmt *flow_stmt_alloc (const struct location *loc,const char *name); +struct objref_stmt {uint32_t type;struct expr *expr;
@@ -229,6 +235,7 @@ struct xt_stmt {* @STMT_NOTRACK: notrack statement * @STMT_OBJREF: stateful object reference statement * @STMT_EXTHDR: extension header statement +* @STMT_FLOW_OFFLOAD: flow offload statement */ enum stmt_types {STMT_INVALID ,
@@ -254,6 +261,7 @@ enum stmt_types {STMT_NOTRACK ,STMT_OBJREF ,STMT_EXTHDR , + STMT_FLOW_OFFLOAD, };/**
@@ -314,6 +322,7 @@ struct stmt {struct fwd_stmt fwd;struct xt_stmt xt;struct objref_stmt objref; +struct flow_stmt flow; }; };
src/ct.c +23 -0
@@ -456,3 +456,26 @@ struct stmt *notrack_stmt_alloc(const struct location *loc) {return stmt_alloc (loc, ¬rack_stmt_ops); } + +static void flow_offload_stmt_print (const struct stmt *stmt, +struct output_ctx *octx) +{ +printf ("flow offload @%s" , stmt->flow.table_name); +} + +static const struct stmt_ops flow_offload_stmt_ops = { + .type =STMT_FLOW_OFFLOAD , + .name ="flow_offload" , + .print = flow_offload_stmt_print, +}; + +struct stmt *flow_offload_stmt_alloc (const struct location *loc, +const char *table_name) +{ +struct stmt *stmt; + + stmt =stmt_alloc (loc, &flow_offload_stmt_ops); + stmt->flow.table_name = table_name; + +return stmt; +}
src/evaluate.c +1 -0
@@ -2773,6 +2773,7 @@ int stmt_evaluate(struct eval_ctx *ctx, struct stmt *stmt)case STMT_LIMIT :case STMT_QUOTA :case STMT_NOTRACK : +case STMT_FLOW_OFFLOAD :return 0 ;case STMT_EXPRESSION :return stmt_evaluate_expr (ctx, stmt);
src/netlink_delinearize.c +11 -0
@@ -677,6 +677,16 @@ static void netlink_parse_notrack(struct netlink_parse_ctx *ctx, ctx->stmt =notrack_stmt_alloc (loc); } +static void netlink_parse_flow_offload (struct netlink_parse_ctx *ctx, +const struct location *loc, +const struct nftnl_expr *nle) +{ +const char *table_name; + + table_name =xstrdup (nftnl_expr_get_str (nle,NFTNL_EXPR_FLOW_TABLE_NAME )); + ctx->stmt =flow_offload_stmt_alloc (loc, table_name); +} +static void netlink_parse_ct_stmt (struct netlink_parse_ctx *ctx,const struct location *loc,const struct nftnl_expr *nle)
@@ -1252,6 +1262,7 @@ static const struct { { .name = "hash", .parse = netlink_parse_hash }, { .name = "fib", .parse = netlink_parse_fib }, { .name = "tcpopt", .parse = netlink_parse_exthdr }, + { .name = "flow_offload", .parse = netlink_parse_flow_offload }, }; static int netlink_parse_expr(const struct nftnl_expr *nle,
src/netlink_linearize.c +13 -0
@@ -1201,6 +1201,17 @@ static void netlink_gen_notrack_stmt(struct netlink_linearize_ctx *ctx,nftnl_rule_add_expr (ctx->nlr, nle); } +static void netlink_gen_flow_offload_stmt (struct netlink_linearize_ctx *ctx, +const struct stmt *stmt) +{ +struct nftnl_expr *nle; + + nle =alloc_nft_expr ("flow_offload" ); +nftnl_expr_set_str (nle,NFTNL_EXPR_FLOW_TABLE_NAME , + stmt->flow.table_name); +nftnl_rule_add_expr (ctx->nlr, nle); +} +static void netlink_gen_set_stmt (struct netlink_linearize_ctx *ctx,const struct stmt *stmt) {
@@ -1300,6 +1311,8 @@ static void netlink_gen_stmt(struct netlink_linearize_ctx *ctx,break ;case STMT_NOTRACK :return netlink_gen_notrack_stmt (ctx, stmt); +case STMT_FLOW_OFFLOAD : +return netlink_gen_flow_offload_stmt (ctx, stmt);case STMT_OBJREF :return netlink_gen_objref_stmt (ctx, stmt);default :
src/parser_bison.y +5 -0
@@ -247,6 +247,7 @@ int nft_lex(void *, void *, void *); %token SIZE "size" %token FLOW "flow" +%token OFFLOAD "offload" %token METER "meter" %token METERS "meters"
@@ -3378,6 +3379,10 @@ meta_stmt : META meta_key SET stmt_expr { $$ = notrack_stmt_alloc(&@$); } + | FLOW OFFLOAD AT string + { + $$ = flow_offload_stmt_alloc(&@$, $4); + } ; offset_opt : /* empty */ { $$ = 0; }
src/scanner.l +1 -0
@@ -295,6 +295,7 @@ addrstring ({macaddr}|{ip4addr}|{ip6addr}) "memory" { return MEMORY; } "flow" { return FLOW; } +"offload" { return OFFLOAD; } "meter" { return METER; } "meters" { return METERS; }
Signature · 1 line
2.11.0
Pablo Neira Ayuso
maintainerPATCH 5/6 tests: shell: add flowtable tests
In reply to Pablo Neira AyusoAdd basic flowtable tests.
--- tests/shell/run-tests.sh | 4 ++- tests/shell/testcases/flowtable/0001flowtable_0 | 33 ++++++++++++++++++++++ .../testcases/flowtable/0002create_flowtable_0 | 12 ++++++++ .../testcases/flowtable/0003add_after_flush_0 | 8 ++++++ .../testcases/flowtable/0004delete_after_add0 | 6 ++++ .../shell/testcases/flowtable/0005delete_in_use_1 | 9 ++++++ 6 files changed, 71 insertions(+), 1 deletion(-) create mode 100755 tests/shell/testcases/flowtable/0001flowtable_0 create mode 100755 tests/shell/testcases/flowtable/0002create_flowtable_0 create mode 100755 tests/shell/testcases/flowtable/0003add_after_flush_0 create mode 100755 tests/shell/testcases/flowtable/0004delete_after_add0 create mode 100755 tests/shell/testcases/flowtable/0005delete_in_use_1
tests/shell/run-tests.sh +3 -1
@@ -68,7 +68,9 @@ kernel_cleanup() { nft_set_hash nft_set_rbtree nft_set_bitmap \ nft_chain_nat_ipv4 nft_chain_nat_ipv6 \ nf_tables_inet nf_tables_bridge nf_tables_arp \ - nf_tables_ipv4 nf_tables_ipv6 nf_tables + nf_tables_ipv4 nf_tables_ipv6 nf_tables \ + nf_flow_table nf_flow_table_ipv4 nf_flow_tables_ipv6 \ + nf_flow_table_inet nft_flow_offload } find_tests() {
tests/shell/testcases/flowtable/0001flowtable_0 new file +33 -0
@@ -0,0 +1,33 @@ +#!/bin/bash + +tmpfile=$(mktemp) +if [ ! -w $tmpfile ] ; then + echo "Failed to create tmp file" >&2 + exit 0 +fi + +trap "rm -rf $tmpfile" EXIT # cleanup if aborted + + +EXPECTED='table inet t { + flowtable f { + hook ingress priority 10 + devices = { eth0, wlan0 } + } + + chain c { + flow offload @f + } +}' + +echo "$EXPECTED" > $tmpfile +set -e +$NFT -f $tmpfile + +GET="$($NFT list ruleset)" + +if [ "$EXPECTED" != "$GET" ] ; then + DIFF="$(which diff)" + [ -x $DIFF ] && $DIFF -u <(echo "$EXPECTED") <(echo "$GET") + exit 1 +fi
tests/shell/testcases/flowtable/0002create_flowtable_0 new file +12 -0
@@ -0,0 +1,12 @@ +#!/bin/bash + +set -e +$NFT add table t +$NFT add flowtable t f { hook ingress priority 10 \; devices = { eth0, wlan0 }\; } +if $NFT create flowtable t f { hook ingress priority 10 \; devices = { eth0, wlan0 }\; } 2>/dev/null ; then + echo "E: flowtable creation not failing on existing set" >&2 + exit 1 +fi +$NFT add flowtable t f { hook ingress priority 10 \; devices = { eth0, wlan0 }\; } + +exit 0
tests/shell/testcases/flowtable/0003add_after_flush_0 new file +8 -0
@@ -0,0 +1,8 @@ +#!/bin/bash + +set -e +$NFT add table x +$NFT add flowtable x y { hook ingress priority 0\; devices = { eth0, wlan0 }\;} +$NFT flush ruleset +$NFT add table x +$NFT add flowtable x y { hook ingress priority 0\; devices = { eth0, wlan0 }\;}
tests/shell/testcases/flowtable/0004delete_after_add0 new file +6 -0
@@ -0,0 +1,6 @@ +#!/bin/bash + +set -e +$NFT add table x +$NFT add flowtable x y { hook ingress priority 0\; devices = { eth0, wlan0 }\;} +$NFT delete flowtable x y
tests/shell/testcases/flowtable/0005delete_in_use_1 new file +9 -0
@@ -0,0 +1,9 @@ +#!/bin/bash + +set -e +$NFT add table x +$NFT add chain x x +$NFT add flowtable x y { hook ingress priority 0\; devices = { eth0, wlan0 }\;} +$NFT add rule x x flow offload @y +$NFT delete flowtable x y +echo "E: delete flowtable in use"
Signature · 1 line
2.11.0
Pablo Neira Ayuso
maintainerPATCH 6/6 doc: nft: document flowtable
In reply to Pablo Neira AyusoDocument the new flowtable objects available since Linux kernel 4.16-rc.
--- doc/nft.xml | 103 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 103 insertions(+)
doc/nft.xml +103 -0
@@ -1160,6 +1160,91 @@ filter input iif $int_ifs accept </refsect1> <refsect1> + <title>Flowtables</title> + <para> + <cmdsynopsis> + <group choice="req"> + <arg>add</arg> + <arg>create</arg> + </group> + <command>flowtable</command> + <arg choice="opt"><replaceable>family</replaceable></arg> + <arg choice="plain"><replaceable>table</replaceable></arg> + <arg choice="plain"><replaceable>flowtable</replaceable></arg> + <arg choice="req"> + hook <replaceable>hook</replaceable> + priority <replaceable>priority</replaceable> ; + devices = { <replaceable>device</replaceable>[,...] } ; + </arg> + </cmdsynopsis> + <cmdsynopsis> + <group choice="req"> + <arg>delete</arg> + <arg>list</arg> + </group> + <command>flowtable</command> + <arg choice="opt"><replaceable>family</replaceable></arg> + <replaceable>table</replaceable> + <replaceable>flowtable</replaceable> + </cmdsynopsis> + </para> + + <para> + Flowtables allow you to accelerate packet forwarding in software. + Flowtables entries are represented through a tuple that is composed of the + input interface, source and destination address, source and destination + port; and layer 3/4 protocols. Each entry also caches the destination + interface and the gateway address - to update the destination link-layer + address - to forward packets. The ttl and hoplimit fields are also + decremented. Hence, flowtables provides an alternative path that allow + packets to bypass the classic forwarding path. Flowtables reside in the + ingress hook, that is located before the prerouting hook. You can select + what flows you want to offload through the <literal>flow offload</literal> + expression from the <literal>forward</literal> chain. Flowtables are + identified by their address family and their name. The address family + must be one of + + <simplelist type="inline"> + <member><literal>ip</literal></member> + <member><literal>ip6</literal></member> + <member><literal>inet</literal></member> + </simplelist>. + + The <literal>inet</literal> address family is a dummy family which is used to create + hybrid IPv4/IPv6 tables. + + When no address family is specified, <literal>ip</literal> is used by default. + </para> + + <variablelist> + <varlistentry> + <term><option>add</option></term> + <listitem> + <para> + Add a new flowtable for the given family with the given name. + </para> + </listitem> + </varlistentry> + <varlistentry> + <term><option>delete</option></term> + <listitem> + <para> + Delete the specified flowtable. + </para> + </listitem> + </varlistentry> + <varlistentry> + <term><option>list</option></term> + <listitem> + <para> + List all flowtables. + </para> + </listitem> + </varlistentry> + </variablelist> + </refsect1> + + <refsect1> <title>Stateful objects</title> <para> <cmdsynopsis>
@@ -4917,6 +5002,24 @@ add rule nat prerouting tcp dport 22 redirect to :2222 </example> </para> </refsect2> + + <refsect2> + <title>Flow offload statement</title> + <para> + A flow offload statement allows us to select what flows + you want to accelerate forwarding through layer 3 network + stack bypass. You have to specify the flowtable name where + you want to offload this flow. + </para> + <para> + <cmdsynopsis> + <command>flow offload</command> + <literal>@flowtable</literal> + </cmdsynopsis> + </para> + + </refsect2> + <refsect2> <title>Queue statement</title> <para>
Signature · 1 line
2.11.0