2
0

203-src-flow-offload-support.patch 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. From: Pablo Neira Ayuso <[email protected]>
  2. Date: Sun, 3 Dec 2017 21:27:03 +0100
  3. Subject: [PATCH] src: flow offload support
  4. This patch allows us to refer to existing flowtables:
  5. # nft add rule x x flow offload @m
  6. Packets matching this rule create an entry in the flow table 'm', hence,
  7. follow up packets that get to the flowtable at ingress bypass the
  8. classic forwarding path.
  9. Signed-off-by: Pablo Neira Ayuso <[email protected]>
  10. ---
  11. --- a/include/ct.h
  12. +++ b/include/ct.h
  13. @@ -29,6 +29,8 @@ extern struct expr *ct_expr_alloc(const
  14. extern void ct_expr_update_type(struct proto_ctx *ctx, struct expr *expr);
  15. extern struct stmt *notrack_stmt_alloc(const struct location *loc);
  16. +extern struct stmt *flow_offload_stmt_alloc(const struct location *loc,
  17. + const char *table_name);
  18. extern const struct datatype ct_dir_type;
  19. extern const struct datatype ct_state_type;
  20. --- a/include/statement.h
  21. +++ b/include/statement.h
  22. @@ -10,6 +10,12 @@ extern struct stmt *expr_stmt_alloc(cons
  23. extern struct stmt *verdict_stmt_alloc(const struct location *loc,
  24. struct expr *expr);
  25. +struct flow_stmt {
  26. + const char *table_name;
  27. +};
  28. +
  29. +struct stmt *flow_stmt_alloc(const struct location *loc, const char *name);
  30. +
  31. struct objref_stmt {
  32. uint32_t type;
  33. struct expr *expr;
  34. @@ -231,6 +237,7 @@ extern struct stmt *xt_stmt_alloc(const
  35. * @STMT_NOTRACK: notrack statement
  36. * @STMT_OBJREF: stateful object reference statement
  37. * @STMT_EXTHDR: extension header statement
  38. + * @STMT_FLOW_OFFLOAD: flow offload statement
  39. */
  40. enum stmt_types {
  41. STMT_INVALID,
  42. @@ -256,6 +263,7 @@ enum stmt_types {
  43. STMT_NOTRACK,
  44. STMT_OBJREF,
  45. STMT_EXTHDR,
  46. + STMT_FLOW_OFFLOAD,
  47. };
  48. /**
  49. @@ -316,6 +324,7 @@ struct stmt {
  50. struct fwd_stmt fwd;
  51. struct xt_stmt xt;
  52. struct objref_stmt objref;
  53. + struct flow_stmt flow;
  54. };
  55. };
  56. --- a/src/ct.c
  57. +++ b/src/ct.c
  58. @@ -456,3 +456,26 @@ struct stmt *notrack_stmt_alloc(const st
  59. {
  60. return stmt_alloc(loc, &notrack_stmt_ops);
  61. }
  62. +
  63. +static void flow_offload_stmt_print(const struct stmt *stmt,
  64. + struct output_ctx *octx)
  65. +{
  66. + printf("flow offload @%s", stmt->flow.table_name);
  67. +}
  68. +
  69. +static const struct stmt_ops flow_offload_stmt_ops = {
  70. + .type = STMT_FLOW_OFFLOAD,
  71. + .name = "flow_offload",
  72. + .print = flow_offload_stmt_print,
  73. +};
  74. +
  75. +struct stmt *flow_offload_stmt_alloc(const struct location *loc,
  76. + const char *table_name)
  77. +{
  78. + struct stmt *stmt;
  79. +
  80. + stmt = stmt_alloc(loc, &flow_offload_stmt_ops);
  81. + stmt->flow.table_name = table_name;
  82. +
  83. + return stmt;
  84. +}
  85. --- a/src/evaluate.c
  86. +++ b/src/evaluate.c
  87. @@ -2773,6 +2773,7 @@ int stmt_evaluate(struct eval_ctx *ctx,
  88. case STMT_LIMIT:
  89. case STMT_QUOTA:
  90. case STMT_NOTRACK:
  91. + case STMT_FLOW_OFFLOAD:
  92. return 0;
  93. case STMT_EXPRESSION:
  94. return stmt_evaluate_expr(ctx, stmt);
  95. --- a/src/netlink_delinearize.c
  96. +++ b/src/netlink_delinearize.c
  97. @@ -680,6 +680,16 @@ static void netlink_parse_notrack(struct
  98. ctx->stmt = notrack_stmt_alloc(loc);
  99. }
  100. +static void netlink_parse_flow_offload(struct netlink_parse_ctx *ctx,
  101. + const struct location *loc,
  102. + const struct nftnl_expr *nle)
  103. +{
  104. + const char *table_name;
  105. +
  106. + table_name = xstrdup(nftnl_expr_get_str(nle, NFTNL_EXPR_FLOW_TABLE_NAME));
  107. + ctx->stmt = flow_offload_stmt_alloc(loc, table_name);
  108. +}
  109. +
  110. static void netlink_parse_ct_stmt(struct netlink_parse_ctx *ctx,
  111. const struct location *loc,
  112. const struct nftnl_expr *nle)
  113. @@ -1255,6 +1265,7 @@ static const struct {
  114. { .name = "hash", .parse = netlink_parse_hash },
  115. { .name = "fib", .parse = netlink_parse_fib },
  116. { .name = "tcpopt", .parse = netlink_parse_exthdr },
  117. + { .name = "flow_offload", .parse = netlink_parse_flow_offload },
  118. };
  119. static int netlink_parse_expr(const struct nftnl_expr *nle,
  120. --- a/src/netlink_linearize.c
  121. +++ b/src/netlink_linearize.c
  122. @@ -1201,6 +1201,17 @@ static void netlink_gen_notrack_stmt(str
  123. nftnl_rule_add_expr(ctx->nlr, nle);
  124. }
  125. +static void netlink_gen_flow_offload_stmt(struct netlink_linearize_ctx *ctx,
  126. + const struct stmt *stmt)
  127. +{
  128. + struct nftnl_expr *nle;
  129. +
  130. + nle = alloc_nft_expr("flow_offload");
  131. + nftnl_expr_set_str(nle, NFTNL_EXPR_FLOW_TABLE_NAME,
  132. + stmt->flow.table_name);
  133. + nftnl_rule_add_expr(ctx->nlr, nle);
  134. +}
  135. +
  136. static void netlink_gen_set_stmt(struct netlink_linearize_ctx *ctx,
  137. const struct stmt *stmt)
  138. {
  139. @@ -1300,6 +1311,8 @@ static void netlink_gen_stmt(struct netl
  140. break;
  141. case STMT_NOTRACK:
  142. return netlink_gen_notrack_stmt(ctx, stmt);
  143. + case STMT_FLOW_OFFLOAD:
  144. + return netlink_gen_flow_offload_stmt(ctx, stmt);
  145. case STMT_OBJREF:
  146. return netlink_gen_objref_stmt(ctx, stmt);
  147. default:
  148. --- a/src/parser_bison.y
  149. +++ b/src/parser_bison.y
  150. @@ -248,6 +248,7 @@ int nft_lex(void *, void *, void *);
  151. %token SIZE "size"
  152. %token FLOW "flow"
  153. +%token OFFLOAD "offload"
  154. %token METER "meter"
  155. %token METERS "meters"
  156. @@ -3384,6 +3385,10 @@ meta_stmt : META meta_key SET stmt_expr
  157. {
  158. $$ = notrack_stmt_alloc(&@$);
  159. }
  160. + | FLOW OFFLOAD AT string
  161. + {
  162. + $$ = flow_offload_stmt_alloc(&@$, $4);
  163. + }
  164. ;
  165. offset_opt : /* empty */ { $$ = 0; }
  166. --- a/src/scanner.l
  167. +++ b/src/scanner.l
  168. @@ -296,6 +296,7 @@ addrstring ({macaddr}|{ip4addr}|{ip6addr
  169. "memory" { return MEMORY; }
  170. "flow" { return FLOW; }
  171. +"offload" { return OFFLOAD; }
  172. "meter" { return METER; }
  173. "meters" { return METERS; }