9219d1
From b74a558d0a5e4c217f966d9615611e3dca1d6c23 Mon Sep 17 00:00:00 2001
9219d1
From: Mark Michelson <mmichels@redhat.com>
9219d1
Date: Mon, 27 Jul 2020 16:11:56 -0400
9219d1
Subject: [PATCH 12/22] Used nested actions in ct_commit
9219d1
9219d1
ct_commit allows for ct_label and ct_mark to be set within. However,
9219d1
there are some restrictions with the current implementation:
9219d1
9219d1
* It is not possible to address the indiviual bits within the ct_mark or
9219d1
   ct_label.
9219d1
* It is not possible to set these to the value of a register. Only
9219d1
   explicit integer setting can be used.
9219d1
9219d1
With this change, ct_commit now can have arbitrary nested actions
9219d1
inside. This makes it similar to how the "exec" option works in OVS's
9219d1
ct() action.
9219d1
9219d1
ct_commit now also sets a writeability scope so that ct_mark and
9219d1
ct_label are the only symbols that are writeable. The positive side
9219d1
effect is that ct_mark and ct_label are no longer writeable except for
9219d1
inside ct_commit.
9219d1
9219d1
In this commit, the only noticeable effect is that it allows for
9219d1
slightly more expressive setting of ct_label.blocked. A future commit
9219d1
will take further advantage of this.
9219d1
9219d1
Signed-off-by: Mark Michelson <mmichels@redhat.com>
9219d1
---
9219d1
 include/ovn/actions.h |   9 +---
9219d1
 include/ovn/expr.h    |   1 +
9219d1
 lib/actions.c         | 110 +++++++-----------------------------------
9219d1
 lib/logical-fields.c  |   9 ++--
9219d1
 northd/ovn-northd.c   |   8 +--
9219d1
 ovn-sb.xml            |  11 +++--
9219d1
 tests/ovn.at          |  59 ++++++++++++----------
9219d1
 7 files changed, 72 insertions(+), 135 deletions(-)
9219d1
9219d1
diff --git a/include/ovn/actions.h b/include/ovn/actions.h
9219d1
index 34ba0d880..636cb4bc1 100644
9219d1
--- a/include/ovn/actions.h
9219d1
+++ b/include/ovn/actions.h
9219d1
@@ -57,7 +57,7 @@ struct ovn_extend_table;
9219d1
     OVNACT(EXCHANGE,          ovnact_move)            \
9219d1
     OVNACT(DEC_TTL,           ovnact_null)            \
9219d1
     OVNACT(CT_NEXT,           ovnact_ct_next)         \
9219d1
-    OVNACT(CT_COMMIT,         ovnact_ct_commit)       \
9219d1
+    OVNACT(CT_COMMIT,         ovnact_nest)            \
9219d1
     OVNACT(CT_DNAT,           ovnact_ct_nat)          \
9219d1
     OVNACT(CT_SNAT,           ovnact_ct_nat)          \
9219d1
     OVNACT(CT_LB,             ovnact_ct_lb)           \
9219d1
@@ -222,13 +222,6 @@ struct ovnact_ct_next {
9219d1
     uint8_t ltable;                /* Logical table ID of next table. */
9219d1
 };
9219d1
 
9219d1
-/* OVNACT_CT_COMMIT. */
9219d1
-struct ovnact_ct_commit {
9219d1
-    struct ovnact ovnact;
9219d1
-    uint32_t ct_mark, ct_mark_mask;
9219d1
-    ovs_be128 ct_label, ct_label_mask;
9219d1
-};
9219d1
-
9219d1
 /* OVNACT_CT_DNAT, OVNACT_CT_SNAT. */
9219d1
 struct ovnact_ct_nat {
9219d1
     struct ovnact ovnact;
9219d1
diff --git a/include/ovn/expr.h b/include/ovn/expr.h
9219d1
index 11bfdad5b..b34fb0e81 100644
9219d1
--- a/include/ovn/expr.h
9219d1
+++ b/include/ovn/expr.h
9219d1
@@ -85,6 +85,7 @@ enum expr_level {
9219d1
 
9219d1
 enum expr_write_scope {
9219d1
     WR_DEFAULT   = (1 << 0), /* Writeable at "global" level */
9219d1
+    WR_CT_COMMIT = (1 << 1), /* Writeable in "ct_commit" action */
9219d1
 };
9219d1
 
9219d1
 const char *expr_level_to_string(enum expr_level);
9219d1
diff --git a/lib/actions.c b/lib/actions.c
9219d1
index 460ab0cf5..79ac79a95 100644
9219d1
--- a/lib/actions.c
9219d1
+++ b/lib/actions.c
9219d1
@@ -200,6 +200,15 @@ struct action_context {
9219d1
 
9219d1
 static void parse_actions(struct action_context *, enum lex_type sentinel);
9219d1
 
9219d1
+static void parse_nested_action(struct action_context *ctx,
9219d1
+                                enum ovnact_type type,
9219d1
+                                const char *prereq,
9219d1
+                                enum expr_write_scope scope);
9219d1
+
9219d1
+static void format_nested_action(const struct ovnact_nest *on,
9219d1
+                                 const char *name,
9219d1
+                                 struct ds *s);
9219d1
+
9219d1
 static bool
9219d1
 action_parse_field(struct action_context *ctx,
9219d1
                    int n_bits, bool rw, struct expr_field *f)
9219d1
@@ -618,125 +627,42 @@ ovnact_ct_next_free(struct ovnact_ct_next *a OVS_UNUSED)
9219d1
 {
9219d1
 }
9219d1
 
9219d1
-static void
9219d1
-parse_ct_commit_arg(struct action_context *ctx,
9219d1
-                    struct ovnact_ct_commit *cc)
9219d1
-{
9219d1
-    if (lexer_match_id(ctx->lexer, "ct_mark")) {
9219d1
-        if (!lexer_force_match(ctx->lexer, LEX_T_EQUALS)) {
9219d1
-            return;
9219d1
-        }
9219d1
-        if (ctx->lexer->token.type == LEX_T_INTEGER) {
9219d1
-            cc->ct_mark = ntohll(ctx->lexer->token.value.integer);
9219d1
-            cc->ct_mark_mask = UINT32_MAX;
9219d1
-        } else if (ctx->lexer->token.type == LEX_T_MASKED_INTEGER) {
9219d1
-            cc->ct_mark = ntohll(ctx->lexer->token.value.integer);
9219d1
-            cc->ct_mark_mask = ntohll(ctx->lexer->token.mask.integer);
9219d1
-        } else {
9219d1
-            lexer_syntax_error(ctx->lexer, "expecting integer");
9219d1
-            return;
9219d1
-        }
9219d1
-        lexer_get(ctx->lexer);
9219d1
-    } else if (lexer_match_id(ctx->lexer, "ct_label")) {
9219d1
-        if (!lexer_force_match(ctx->lexer, LEX_T_EQUALS)) {
9219d1
-            return;
9219d1
-        }
9219d1
-        if (ctx->lexer->token.type == LEX_T_INTEGER) {
9219d1
-            cc->ct_label = ctx->lexer->token.value.be128_int;
9219d1
-            cc->ct_label_mask = OVS_BE128_MAX;
9219d1
-        } else if (ctx->lexer->token.type == LEX_T_MASKED_INTEGER) {
9219d1
-            cc->ct_label = ctx->lexer->token.value.be128_int;
9219d1
-            cc->ct_label_mask = ctx->lexer->token.mask.be128_int;
9219d1
-        } else {
9219d1
-            lexer_syntax_error(ctx->lexer, "expecting integer");
9219d1
-            return;
9219d1
-        }
9219d1
-        lexer_get(ctx->lexer);
9219d1
-    } else {
9219d1
-        lexer_syntax_error(ctx->lexer, NULL);
9219d1
-    }
9219d1
-}
9219d1
-
9219d1
 static void
9219d1
 parse_CT_COMMIT(struct action_context *ctx)
9219d1
 {
9219d1
-    add_prerequisite(ctx, "ip");
9219d1
 
9219d1
-    struct ovnact_ct_commit *ct_commit = ovnact_put_CT_COMMIT(ctx->ovnacts);
9219d1
-    if (lexer_match(ctx->lexer, LEX_T_LPAREN)) {
9219d1
-        while (!lexer_match(ctx->lexer, LEX_T_RPAREN)) {
9219d1
-            parse_ct_commit_arg(ctx, ct_commit);
9219d1
-            if (ctx->lexer->error) {
9219d1
-                return;
9219d1
-            }
9219d1
-            lexer_match(ctx->lexer, LEX_T_COMMA);
9219d1
-        }
9219d1
-    }
9219d1
+    parse_nested_action(ctx, OVNACT_CT_COMMIT, "ip",
9219d1
+                        WR_CT_COMMIT);
9219d1
 }
9219d1
 
9219d1
 static void
9219d1
-format_CT_COMMIT(const struct ovnact_ct_commit *cc, struct ds *s)
9219d1
+format_CT_COMMIT(const struct ovnact_nest *on, struct ds *s)
9219d1
 {
9219d1
-    ds_put_cstr(s, "ct_commit(");
9219d1
-    if (cc->ct_mark_mask) {
9219d1
-        ds_put_format(s, "ct_mark=%#"PRIx32, cc->ct_mark);
9219d1
-        if (cc->ct_mark_mask != UINT32_MAX) {
9219d1
-            ds_put_format(s, "/%#"PRIx32, cc->ct_mark_mask);
9219d1
-        }
9219d1
-    }
9219d1
-    if (!ovs_be128_is_zero(cc->ct_label_mask)) {
9219d1
-        if (ds_last(s) != '(') {
9219d1
-            ds_put_cstr(s, ", ");
9219d1
-        }
9219d1
-
9219d1
-        ds_put_format(s, "ct_label=");
9219d1
-        ds_put_hex(s, &cc->ct_label, sizeof cc->ct_label);
9219d1
-        if (!ovs_be128_equals(cc->ct_label_mask, OVS_BE128_MAX)) {
9219d1
-            ds_put_char(s, '/');
9219d1
-            ds_put_hex(s, &cc->ct_label_mask, sizeof cc->ct_label_mask);
9219d1
-        }
9219d1
-    }
9219d1
-    if (!ds_chomp(s, '(')) {
9219d1
-        ds_put_char(s, ')');
9219d1
-    }
9219d1
-    ds_put_char(s, ';');
9219d1
+    format_nested_action(on, "ct_commit", s);
9219d1
 }
9219d1
 
9219d1
 static void
9219d1
-encode_CT_COMMIT(const struct ovnact_ct_commit *cc,
9219d1
+encode_CT_COMMIT(const struct ovnact_nest *on,
9219d1
                  const struct ovnact_encode_params *ep OVS_UNUSED,
9219d1
                  struct ofpbuf *ofpacts)
9219d1
 {
9219d1
     struct ofpact_conntrack *ct = ofpact_put_CT(ofpacts);
9219d1
     ct->flags = NX_CT_F_COMMIT;
9219d1
     ct->recirc_table = NX_CT_RECIRC_NONE;
9219d1
-    ct->zone_src.field = mf_from_id(MFF_LOG_CT_ZONE);
9219d1
+    ct->zone_src.field = ep->is_switch
9219d1
+        ? mf_from_id(MFF_LOG_CT_ZONE)
9219d1
+        : mf_from_id(MFF_LOG_DNAT_ZONE);
9219d1
     ct->zone_src.ofs = 0;
9219d1
     ct->zone_src.n_bits = 16;
9219d1
 
9219d1
     size_t set_field_offset = ofpacts->size;
9219d1
     ofpbuf_pull(ofpacts, set_field_offset);
9219d1
 
9219d1
-    if (cc->ct_mark_mask) {
9219d1
-        const ovs_be32 value = htonl(cc->ct_mark);
9219d1
-        const ovs_be32 mask = htonl(cc->ct_mark_mask);
9219d1
-        ofpact_put_set_field(ofpacts, mf_from_id(MFF_CT_MARK), &value, &mask);
9219d1
-    }
9219d1
-
9219d1
-    if (!ovs_be128_is_zero(cc->ct_label_mask)) {
9219d1
-        ofpact_put_set_field(ofpacts, mf_from_id(MFF_CT_LABEL), &cc->ct_label,
9219d1
-                             &cc->ct_label_mask);
9219d1
-    }
9219d1
-
9219d1
+    ovnacts_encode(on->nested, on->nested_len, ep, ofpacts);
9219d1
     ofpacts->header = ofpbuf_push_uninit(ofpacts, set_field_offset);
9219d1
     ct = ofpacts->header;
9219d1
     ofpact_finish(ofpacts, &ct->ofpact);
9219d1
 }
9219d1
-
9219d1
-static void
9219d1
-ovnact_ct_commit_free(struct ovnact_ct_commit *cc OVS_UNUSED)
9219d1
-{
9219d1
-}
9219d1
 
9219d1
 static void
9219d1
 parse_ct_nat(struct action_context *ctx, const char *name,
9219d1
diff --git a/lib/logical-fields.c b/lib/logical-fields.c
9219d1
index 8639523ea..fde53a47e 100644
9219d1
--- a/lib/logical-fields.c
9219d1
+++ b/lib/logical-fields.c
9219d1
@@ -123,10 +123,13 @@ ovn_init_symtab(struct shash *symtab)
9219d1
                              flags_str);
9219d1
 
9219d1
     /* Connection tracking state. */
9219d1
-    expr_symtab_add_field(symtab, "ct_mark", MFF_CT_MARK, NULL, false);
9219d1
+    expr_symtab_add_field_scoped(symtab, "ct_mark", MFF_CT_MARK, NULL, false,
9219d1
+                                 WR_CT_COMMIT);
9219d1
 
9219d1
-    expr_symtab_add_field(symtab, "ct_label", MFF_CT_LABEL, NULL, false);
9219d1
-    expr_symtab_add_subfield(symtab, "ct_label.blocked", NULL, "ct_label[0]");
9219d1
+    expr_symtab_add_field_scoped(symtab, "ct_label", MFF_CT_LABEL, NULL, false,
9219d1
+                                 WR_CT_COMMIT);
9219d1
+    expr_symtab_add_subfield_scoped(symtab, "ct_label.blocked", NULL,
9219d1
+                                    "ct_label[0]", WR_CT_COMMIT);
9219d1
 
9219d1
     expr_symtab_add_field(symtab, "ct_state", MFF_CT_STATE, NULL, false);
9219d1
 
9219d1
diff --git a/northd/ovn-northd.c b/northd/ovn-northd.c
9219d1
index 6375aee8d..44e7d9365 100644
9219d1
--- a/northd/ovn-northd.c
9219d1
+++ b/northd/ovn-northd.c
9219d1
@@ -5356,7 +5356,7 @@ consider_acl(struct hmap *lflows, struct ovn_datapath *od,
9219d1
             ds_clear(&match);
9219d1
             ds_clear(&actions);
9219d1
             ds_put_cstr(&match, "ct.est && ct_label.blocked == 0");
9219d1
-            ds_put_cstr(&actions, "ct_commit(ct_label=1/1); ");
9219d1
+            ds_put_cstr(&actions, "ct_commit { ct_label.blocked = 1; }; ");
9219d1
             if (!strcmp(acl->action, "reject")) {
9219d1
                 build_reject_acl_rules(od, lflows, stage, acl, &match,
9219d1
                                        &actions, &acl->header_);
9219d1
@@ -5880,9 +5880,11 @@ build_stateful(struct ovn_datapath *od, struct hmap *lflows, struct hmap *lbs)
9219d1
      * any packet that makes it this far is part of a connection we
9219d1
      * want to allow to continue. */
9219d1
     ovn_lflow_add(lflows, od, S_SWITCH_IN_STATEFUL, 100,
9219d1
-                  REGBIT_CONNTRACK_COMMIT" == 1", "ct_commit(ct_label=0/1); next;");
9219d1
+                  REGBIT_CONNTRACK_COMMIT" == 1",
9219d1
+                  "ct_commit { ct_label.blocked = 0; }; next;");
9219d1
     ovn_lflow_add(lflows, od, S_SWITCH_OUT_STATEFUL, 100,
9219d1
-                  REGBIT_CONNTRACK_COMMIT" == 1", "ct_commit(ct_label=0/1); next;");
9219d1
+                  REGBIT_CONNTRACK_COMMIT" == 1",
9219d1
+                  "ct_commit { ct_label.blocked = 0; }; next;");
9219d1
 
9219d1
     /* If REGBIT_CONNTRACK_NAT is set as 1, then packets should just be sent
9219d1
      * through nat (without committing).
9219d1
diff --git a/ovn-sb.xml b/ovn-sb.xml
9219d1
index fc39b2d03..a74d9c3ea 100644
9219d1
--- a/ovn-sb.xml
9219d1
+++ b/ovn-sb.xml
9219d1
@@ -1261,10 +1261,10 @@
9219d1
           

9219d1
         
9219d1
 
9219d1
-        
ct_commit;
9219d1
-        
ct_commit(ct_mark=value[/mask]);
9219d1
-        
ct_commit(ct_label=value[/mask]);
9219d1
-        
ct_commit(ct_mark=value[/mask], ct_label=value[/mask]);
9219d1
+        
ct_commit { };
9219d1
+        
ct_commit { ct_mark=value[/mask]; };
9219d1
+        
ct_commit { ct_label=value[/mask]; };
9219d1
+        
ct_commit { ct_mark=value[/mask]; ct_label=value[/mask]; };
9219d1
         
9219d1
           

9219d1
             Commit the flow to the connection tracking entry associated with it
9219d1
@@ -1276,6 +1276,9 @@
9219d1
             tracking entry. ct_mark is a 32-bit field.
9219d1
             ct_label is a 128-bit field. The value[/mask]
9219d1
             should be specified in hex string if more than 64bits are to be used.
9219d1
+            Registers and other named fields can be used for value.
9219d1
+            ct_mark and ct_label may be sub-addressed
9219d1
+            in order to have specific bits set.
9219d1
           

9219d1
 
9219d1
           

9219d1
diff --git a/tests/ovn.at b/tests/ovn.at
9219d1
index 905112a8d..4c68b77d8 100644
9219d1
--- a/tests/ovn.at
9219d1
+++ b/tests/ovn.at
9219d1
@@ -1045,51 +1045,60 @@ ct_next;
9219d1
     has prereqs ip
9219d1
 
9219d1
 # ct_commit
9219d1
-ct_commit;
9219d1
+ct_commit { };
9219d1
+    formats as ct_commit { drop; };
9219d1
     encodes as ct(commit,zone=NXM_NX_REG13[0..15])
9219d1
     has prereqs ip
9219d1
-ct_commit();
9219d1
-    formats as ct_commit;
9219d1
-    encodes as ct(commit,zone=NXM_NX_REG13[0..15])
9219d1
-    has prereqs ip
9219d1
-ct_commit(ct_mark=1);
9219d1
-    formats as ct_commit(ct_mark=0x1);
9219d1
+ct_commit { ct_mark=1; };
9219d1
+    formats as ct_commit { ct_mark = 1; };
9219d1
     encodes as ct(commit,zone=NXM_NX_REG13[0..15],exec(set_field:0x1->ct_mark))
9219d1
     has prereqs ip
9219d1
-ct_commit(ct_mark=1/1);
9219d1
-    formats as ct_commit(ct_mark=0x1/0x1);
9219d1
+ct_commit { ct_mark=1/1; };
9219d1
+    formats as ct_commit { ct_mark = 1/1; };
9219d1
     encodes as ct(commit,zone=NXM_NX_REG13[0..15],exec(set_field:0x1/0x1->ct_mark))
9219d1
     has prereqs ip
9219d1
-ct_commit(ct_label=1);
9219d1
-    formats as ct_commit(ct_label=0x1);
9219d1
+ct_commit { ct_label=1; };
9219d1
+    formats as ct_commit { ct_label = 1; };
9219d1
     encodes as ct(commit,zone=NXM_NX_REG13[0..15],exec(set_field:0x1->ct_label))
9219d1
     has prereqs ip
9219d1
-ct_commit(ct_label=1/1);
9219d1
-    formats as ct_commit(ct_label=0x1/0x1);
9219d1
+ct_commit { ct_label=1/1; };
9219d1
+    formats as ct_commit { ct_label = 1/1; };
9219d1
     encodes as ct(commit,zone=NXM_NX_REG13[0..15],exec(set_field:0x1/0x1->ct_label))
9219d1
     has prereqs ip
9219d1
-ct_commit(ct_mark=1, ct_label=2);
9219d1
-    formats as ct_commit(ct_mark=0x1, ct_label=0x2);
9219d1
+ct_commit { ct_mark=1; ct_label=2; };
9219d1
+    formats as ct_commit { ct_mark = 1; ct_label = 2; };
9219d1
     encodes as ct(commit,zone=NXM_NX_REG13[0..15],exec(set_field:0x1->ct_mark,set_field:0x2->ct_label))
9219d1
     has prereqs ip
9219d1
 
9219d1
-ct_commit(ct_label=0x01020304050607080910111213141516);
9219d1
-    formats as ct_commit(ct_label=0x1020304050607080910111213141516);
9219d1
+ct_commit { ct_label=0x01020304050607080910111213141516; };
9219d1
+    formats as ct_commit { ct_label = 0x1020304050607080910111213141516; };
9219d1
     encodes as ct(commit,zone=NXM_NX_REG13[0..15],exec(set_field:0x1020304050607080910111213141516->ct_label))
9219d1
     has prereqs ip
9219d1
-ct_commit(ct_label=0x181716151413121110090807060504030201);
9219d1
-    formats as ct_commit(ct_label=0x16151413121110090807060504030201);
9219d1
-    encodes as ct(commit,zone=NXM_NX_REG13[0..15],exec(set_field:0x16151413121110090807060504030201->ct_label))
9219d1
-    has prereqs ip
9219d1
-ct_commit(ct_label=0x1000000000000000000000000000000/0x1000000000000000000000000000000);
9219d1
+ct_commit { ct_label=0x1000000000000000000000000000000/0x1000000000000000000000000000000; };
9219d1
+    formats as ct_commit { ct_label = 0x1000000000000000000000000000000/0x1000000000000000000000000000000; };
9219d1
     encodes as ct(commit,zone=NXM_NX_REG13[0..15],exec(set_field:0x1000000000000000000000000000000/0x1000000000000000000000000000000->ct_label))
9219d1
     has prereqs ip
9219d1
-ct_commit(ct_label=18446744073709551615);
9219d1
-    formats as ct_commit(ct_label=0xffffffffffffffff);
9219d1
+ct_commit { ct_label=18446744073709551615; };
9219d1
+    formats as ct_commit { ct_label = 18446744073709551615; };
9219d1
     encodes as ct(commit,zone=NXM_NX_REG13[0..15],exec(set_field:0xffffffffffffffff->ct_label))
9219d1
     has prereqs ip
9219d1
-ct_commit(ct_label=18446744073709551616);
9219d1
+ct_commit { ct_label[0..47] = 0x00000f040201; ct_label[48..63] = 0x0002; };
9219d1
+    formats as ct_commit { ct_label[0..47] = 0xf040201; ct_label[48..63] = 0x2; };
9219d1
+    encodes as ct(commit,zone=NXM_NX_REG13[0..15],exec(set_field:0xf040201/0xffffffffffff->ct_label,set_field:0x2000000000000/0xffff000000000000->ct_label))
9219d1
+    has prereqs ip
9219d1
+ct_commit { ct_label=18446744073709551616; };
9219d1
     Decimal constants must be less than 2**64.
9219d1
+ct_commit { ct_label=0x181716151413121110090807060504030201; };
9219d1
+    141-bit constant is not compatible with 128-bit field ct_label.
9219d1
+ct_commit { ip4.dst = 192.168.0.1; };
9219d1
+    Field ip4.dst is not modifiable.
9219d1
+
9219d1
+ct_mark = 12345
9219d1
+    Field ct_mark is not modifiable.
9219d1
+ct_label = 0xcafe
9219d1
+    Field ct_label is not modifiable.
9219d1
+ct_label.blocked = 1/1
9219d1
+    Field ct_label.blocked is not modifiable.
9219d1
 
9219d1
 # ct_dnat
9219d1
 ct_dnat;
9219d1
-- 
9219d1
2.26.2
9219d1