]> git.ipfire.org Git - thirdparty/nftables.git/commitdiff
src: flow offload support
authorPablo Neira Ayuso <pablo@netfilter.org>
Sun, 3 Dec 2017 20:27:03 +0000 (21:27 +0100)
committerPablo Neira Ayuso <pablo@netfilter.org>
Mon, 5 Mar 2018 15:30:15 +0000 (16:30 +0100)
This 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.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
include/ct.h
include/statement.h
src/ct.c
src/evaluate.c
src/netlink_delinearize.c
src/netlink_linearize.c
src/parser_bison.y
src/scanner.l

index ec5d55d85dd0623ec29d47d123861b3828fd7e73..2c3392d36c94eabd672697debe7dfbe37189c248 100644 (file)
@@ -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;
index 379d99e4c4a0ee6dccdd6a44a483605ecc84f118..27c735677df2dc7c0d5e377fe27b261d2ad8f18c 100644 (file)
@@ -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;
@@ -231,6 +237,7 @@ extern struct stmt *xt_stmt_alloc(const struct location *loc);
  * @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,
@@ -256,6 +263,7 @@ enum stmt_types {
        STMT_NOTRACK,
        STMT_OBJREF,
        STMT_EXTHDR,
+       STMT_FLOW_OFFLOAD,
 };
 
 /**
@@ -316,6 +324,7 @@ struct stmt {
                struct fwd_stmt         fwd;
                struct xt_stmt          xt;
                struct objref_stmt      objref;
+               struct flow_stmt        flow;
        };
 };
 
index bdf50d7a46d6b14a72c6c950bb99c7680e1557e4..edfe5586ebe65a56090440a2d8ef15d099c6d2c1 100644 (file)
--- a/src/ct.c
+++ b/src/ct.c
@@ -456,3 +456,26 @@ struct stmt *notrack_stmt_alloc(const struct location *loc)
 {
        return stmt_alloc(loc, &notrack_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;
+}
index 51841136766945e6955c852cb2ae8aa89b40cf19..41ba161728af3719d2cf1c61702a5705861098f8 100644 (file)
@@ -2786,6 +2786,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);
index c7df2b434eda54b57d397307af2e2bae11558c18..d65aacf8b61683ec56ab4ee5c604ca8fe1e57ad7 100644 (file)
@@ -686,6 +686,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)
@@ -1294,6 +1304,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,
index 77abdcb8a23ee4b595a38d422e391faed04097a6..5edb2d3d6fe86956e0ac83351ef27f44efaf8fdf 100644 (file)
@@ -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:
index 15d2432a7b1b8c634031c7a20ac722586e899404..b637ab0738c8960f205693c684c6d01a046b895f 100644 (file)
@@ -251,6 +251,7 @@ int nft_lex(void *, void *, void *);
 %token SIZE                    "size"
 
 %token FLOW                    "flow"
+%token OFFLOAD                 "offload"
 %token METER                   "meter"
 %token METERS                  "meters"
 
@@ -3411,6 +3412,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; }
index 79dccda2134603c1870bff19e6c7000bf9e78d54..38e92db070d288b28882a8fb831f9ac865406c37 100644 (file)
@@ -298,6 +298,7 @@ addrstring  ({macaddr}|{ip4addr}|{ip6addr})
 "memory"               { return MEMORY; }
 
 "flow"                 { return FLOW; }
+"offload"              { return OFFLOAD; }
 "meter"                        { return METER; }
 "meters"               { return METERS; }