Setting tag sliver-openvswitch-2.2.90-1
[sliver-openvswitch.git] / lib / learn.c
index 28b8012..8727a55 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2011, 2012 Nicira, Inc.
+ * Copyright (c) 2011, 2012, 2013 Nicira, Inc.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -20,6 +20,7 @@
 
 #include "byte-order.h"
 #include "dynamic-string.h"
+#include "match.h"
 #include "meta-flow.h"
 #include "nx-match.h"
 #include "ofp-actions.h"
@@ -96,12 +97,23 @@ learn_from_openflow(const struct nx_action_learn *nal, struct ofpbuf *ofpacts)
     learn->hard_timeout = ntohs(nal->hard_timeout);
     learn->priority = ntohs(nal->priority);
     learn->cookie = ntohll(nal->cookie);
-    learn->flags = ntohs(nal->flags);
     learn->table_id = nal->table_id;
     learn->fin_idle_timeout = ntohs(nal->fin_idle_timeout);
     learn->fin_hard_timeout = ntohs(nal->fin_hard_timeout);
 
-    if (learn->flags & ~OFPFF_SEND_FLOW_REM || learn->table_id == 0xff) {
+    /* We only support "send-flow-removed" for now. */
+    switch (ntohs(nal->flags)) {
+    case 0:
+        learn->flags = 0;
+        break;
+    case OFPFF_SEND_FLOW_REM:
+        learn->flags = OFPUTIL_FF_SEND_FLOW_REM;
+        break;
+    default:
+        return OFPERR_OFPBAC_BAD_ARGUMENT;
+    }
+
+    if (learn->table_id == 0xff) {
         return OFPERR_OFPBAC_BAD_ARGUMENT;
     }
 
@@ -115,7 +127,7 @@ learn_from_openflow(const struct nx_action_learn *nal, struct ofpbuf *ofpacts)
         }
 
         spec = ofpbuf_put_zeros(ofpacts, sizeof *spec);
-        learn = ofpacts->l2;
+        learn = ofpacts->frame;
         learn->n_specs++;
 
         spec->src_type = header & NX_LEARN_SRC_MASK;
@@ -170,9 +182,9 @@ enum ofperr
 learn_check(const struct ofpact_learn *learn, const struct flow *flow)
 {
     const struct ofpact_learn_spec *spec;
-    struct cls_rule rule;
+    struct match match;
 
-    cls_rule_init_catchall(&rule, 0);
+    match_init_catchall(&match);
     for (spec = learn->specs; spec < &learn->specs[learn->n_specs]; spec++) {
         enum ofperr error;
 
@@ -187,16 +199,16 @@ learn_check(const struct ofpact_learn *learn, const struct flow *flow)
         /* Check the destination. */
         switch (spec->dst_type) {
         case NX_LEARN_DST_MATCH:
-            error = mf_check_src(&spec->dst, &rule.flow);
+            error = mf_check_src(&spec->dst, &match.flow);
             if (error) {
                 return error;
             }
 
-            mf_write_subfield(&spec->dst, &spec->src_imm, &rule);
+            mf_write_subfield(&spec->dst, &spec->src_imm, &match);
             break;
 
         case NX_LEARN_DST_LOAD:
-            error = mf_check_dst(&spec->dst, &rule.flow);
+            error = mf_check_dst(&spec->dst, &match.flow);
             if (error) {
                 return error;
             }
@@ -243,7 +255,7 @@ learn_to_nxast(const struct ofpact_learn *learn, struct ofpbuf *openflow)
     struct nx_action_learn *nal;
     size_t start_ofs;
 
-    start_ofs = openflow->size;
+    start_ofs = ofpbuf_size(openflow);
     nal = ofputil_put_NXAST_LEARN(openflow);
     nal->idle_timeout = htons(learn->idle_timeout);
     nal->hard_timeout = htons(learn->hard_timeout);
@@ -275,12 +287,12 @@ learn_to_nxast(const struct ofpact_learn *learn, struct ofpbuf *openflow)
         }
     }
 
-    if ((openflow->size - start_ofs) % 8) {
-        ofpbuf_put_zeros(openflow, 8 - (openflow->size - start_ofs) % 8);
+    if ((ofpbuf_size(openflow) - start_ofs) % 8) {
+        ofpbuf_put_zeros(openflow, 8 - (ofpbuf_size(openflow) - start_ofs) % 8);
     }
 
     nal = ofpbuf_at_assert(openflow, start_ofs, sizeof *nal);
-    nal->len = htons(openflow->size - start_ofs);
+    nal->len = htons(ofpbuf_size(openflow) - start_ofs);
 }
 
 /* Composes 'fm' so that executing it will implement 'learn' given that the
@@ -297,10 +309,12 @@ learn_execute(const struct ofpact_learn *learn, const struct flow *flow,
 {
     const struct ofpact_learn_spec *spec;
 
-    cls_rule_init_catchall(&fm->cr, learn->priority);
+    match_init_catchall(&fm->match);
+    fm->priority = learn->priority;
     fm->cookie = htonll(0);
     fm->cookie_mask = htonll(0);
     fm->new_cookie = htonll(learn->cookie);
+    fm->modify_cookie = fm->new_cookie != OVS_BE64_MAX;
     fm->table_id = learn->table_id;
     fm->command = OFPFC_MODIFY_STRICT;
     fm->idle_timeout = learn->idle_timeout;
@@ -331,13 +345,12 @@ learn_execute(const struct ofpact_learn *learn, const struct flow *flow,
 
         switch (spec->dst_type) {
         case NX_LEARN_DST_MATCH:
-            mf_write_subfield(&spec->dst, &value, &fm->cr);
+            mf_write_subfield(&spec->dst, &value, &fm->match);
             break;
 
         case NX_LEARN_DST_LOAD:
             for (ofs = 0; ofs < spec->n_bits; ofs += chunk) {
                 struct ofpact_reg_load *load;
-                ovs_be64 value_be;
 
                 chunk = MIN(spec->n_bits - ofs, 64);
 
@@ -345,21 +358,18 @@ learn_execute(const struct ofpact_learn *learn, const struct flow *flow,
                 load->dst.field = spec->dst.field;
                 load->dst.ofs = spec->dst.ofs + ofs;
                 load->dst.n_bits = chunk;
-
-                memset(&value_be, 0, sizeof value_be);
                 bitwise_copy(&value, sizeof value, ofs,
-                             &value_be, sizeof value_be, 0,
+                             &load->subvalue, sizeof load->subvalue, 0,
                              chunk);
-                load->value = ntohll(value_be);
             }
             break;
 
         case NX_LEARN_DST_OUTPUT:
             if (spec->n_bits <= 16
                 || is_all_zeros(value.u8, sizeof value - 2)) {
-                uint16_t port = ntohs(value.be16[7]);
+                ofp_port_t port = u16_to_ofp(ntohs(value.be16[7]));
 
-                if (port < OFPP_MAX
+                if (ofp_to_u16(port) < ofp_to_u16(OFPP_MAX)
                     || port == OFPP_IN_PORT
                     || port == OFPP_FLOOD
                     || port == OFPP_LOCAL
@@ -372,17 +382,36 @@ learn_execute(const struct ofpact_learn *learn, const struct flow *flow,
     }
     ofpact_pad(ofpacts);
 
-    fm->ofpacts = ofpacts->data;
-    fm->ofpacts_len = ofpacts->size;
+    fm->ofpacts = ofpbuf_data(ofpacts);
+    fm->ofpacts_len = ofpbuf_size(ofpacts);
 }
 
-static void
+/* Perform a bitwise-OR on 'wc''s fields that are relevant as sources in
+ * the learn action 'learn'. */
+void
+learn_mask(const struct ofpact_learn *learn, struct flow_wildcards *wc)
+{
+    const struct ofpact_learn_spec *spec;
+    union mf_subvalue value;
+
+    memset(&value, 0xff, sizeof value);
+    for (spec = learn->specs; spec < &learn->specs[learn->n_specs]; spec++) {
+        if (spec->src_type == NX_LEARN_SRC_FIELD) {
+            mf_write_subfield_flow(&spec->src, &value, &wc->masks);
+        }
+    }
+}
+
+/* Returns NULL if successful, otherwise a malloc()'d string describing the
+ * error.  The caller is responsible for freeing the returned string. */
+static char * WARN_UNUSED_RESULT
 learn_parse_load_immediate(const char *s, struct ofpact_learn_spec *spec)
 {
     const char *full_s = s;
     const char *arrow = strstr(s, "->");
     struct mf_subfield dst;
     union mf_subvalue imm;
+    char *error;
 
     memset(&imm, 0, sizeof imm);
     if (s[0] == '0' && (s[1] == 'x' || s[1] == 'X') && arrow) {
@@ -394,7 +423,7 @@ learn_parse_load_immediate(const char *s, struct ofpact_learn_spec *spec)
         for (i = 0; i < n; i++) {
             int hexit = hexit_value(in[-i]);
             if (hexit < 0) {
-                ovs_fatal(0, "%s: bad hex digit in value", full_s);
+                return xasprintf("%s: bad hex digit in value", full_s);
             }
             out[-(i / 2)] |= i % 2 ? hexit << 4 : hexit;
         }
@@ -404,19 +433,19 @@ learn_parse_load_immediate(const char *s, struct ofpact_learn_spec *spec)
     }
 
     if (strncmp(s, "->", 2)) {
-        ovs_fatal(0, "%s: missing `->' following value", full_s);
+        return xasprintf("%s: missing `->' following value", full_s);
     }
     s += 2;
 
-    s = mf_parse_subfield(&dst, s);
-    if (*s != '\0') {
-        ovs_fatal(0, "%s: trailing garbage following destination", full_s);
+    error = mf_parse_subfield(&dst, s);
+    if (error) {
+        return error;
     }
 
     if (!bitwise_is_all_zeros(&imm, sizeof imm, dst.n_bits,
                               (8 * sizeof imm) - dst.n_bits)) {
-        ovs_fatal(0, "%s: value does not fit into %u bits",
-                  full_s, dst.n_bits);
+        return xasprintf("%s: value does not fit into %u bits",
+                         full_s, dst.n_bits);
     }
 
     spec->n_bits = dst.n_bits;
@@ -424,9 +453,12 @@ learn_parse_load_immediate(const char *s, struct ofpact_learn_spec *spec)
     spec->src_imm = imm;
     spec->dst_type = NX_LEARN_DST_LOAD;
     spec->dst = dst;
+    return NULL;
 }
 
-static void
+/* Returns NULL if successful, otherwise a malloc()'d string describing the
+ * error.  The caller is responsible for freeing the returned string. */
+static char * WARN_UNUSED_RESULT
 learn_parse_spec(const char *orig, char *name, char *value,
                  struct ofpact_learn_spec *spec)
 {
@@ -437,7 +469,7 @@ learn_parse_spec(const char *orig, char *name, char *value,
 
         error = mf_parse_value(dst, value, &imm);
         if (error) {
-            ovs_fatal(0, "%s", error);
+            return error;
         }
 
         spec->n_bits = dst->n_bits;
@@ -451,21 +483,23 @@ learn_parse_spec(const char *orig, char *name, char *value,
         spec->dst.n_bits = dst->n_bits;
     } else if (strchr(name, '[')) {
         /* Parse destination and check prerequisites. */
-        if (mf_parse_subfield(&spec->dst, name)[0] != '\0') {
-            ovs_fatal(0, "%s: syntax error after NXM field name `%s'",
-                      orig, name);
+        char *error;
+
+        error = mf_parse_subfield(&spec->dst, name);
+        if (error) {
+            return error;
         }
 
         /* Parse source and check prerequisites. */
         if (value[0] != '\0') {
-            if (mf_parse_subfield(&spec->src, value)[0] != '\0') {
-                ovs_fatal(0, "%s: syntax error after NXM field name `%s'",
-                          orig, value);
+            error = mf_parse_subfield(&spec->src, value);
+            if (error) {
+                return error;
             }
             if (spec->src.n_bits != spec->dst.n_bits) {
-                ovs_fatal(0, "%s: bit widths of %s (%u) and %s (%u) differ",
-                          orig, name, spec->src.n_bits, value,
-                          spec->dst.n_bits);
+                return xasprintf("%s: bit widths of %s (%u) and %s (%u) "
+                                 "differ", orig, name, spec->src.n_bits, value,
+                                 spec->dst.n_bits);
             }
         } else {
             spec->src = spec->dst;
@@ -476,11 +510,18 @@ learn_parse_spec(const char *orig, char *name, char *value,
         spec->dst_type = NX_LEARN_DST_MATCH;
     } else if (!strcmp(name, "load")) {
         if (value[strcspn(value, "[-")] == '-') {
-            learn_parse_load_immediate(value, spec);
+            char *error = learn_parse_load_immediate(value, spec);
+            if (error) {
+                return error;
+            }
         } else {
             struct ofpact_reg_move move;
+            char *error;
 
-            nxm_parse_reg_move(&move, value);
+            error = nxm_parse_reg_move(&move, value);
+            if (error) {
+                return error;
+            }
 
             spec->n_bits = move.src.n_bits;
             spec->src_type = NX_LEARN_SRC_FIELD;
@@ -489,39 +530,29 @@ learn_parse_spec(const char *orig, char *name, char *value,
             spec->dst = move.dst;
         }
     } else if (!strcmp(name, "output")) {
-        if (mf_parse_subfield(&spec->src, value)[0] != '\0') {
-            ovs_fatal(0, "%s: syntax error after NXM field name `%s'",
-                      orig, name);
+        char *error = mf_parse_subfield(&spec->src, value);
+        if (error) {
+            return error;
         }
 
         spec->n_bits = spec->src.n_bits;
         spec->src_type = NX_LEARN_SRC_FIELD;
         spec->dst_type = NX_LEARN_DST_OUTPUT;
     } else {
-        ovs_fatal(0, "%s: unknown keyword %s", orig, name);
+        return xasprintf("%s: unknown keyword %s", orig, name);
     }
+
+    return NULL;
 }
 
-/* Parses 'arg' as a set of arguments to the "learn" action and appends a
- * matching OFPACT_LEARN action to 'ofpacts'.  ovs-ofctl(8) describes the
- * format parsed.
- *
- * Prints an error on stderr and aborts the program if 'arg' syntax is invalid.
- *
- * If 'flow' is nonnull, then it should be the flow from a cls_rule that is
- * the matching rule for the learning action.  This helps to better validate
- * the action's arguments.
- *
- * Modifies 'arg'. */
-void
-learn_parse(char *arg, const struct flow *flow, struct ofpbuf *ofpacts)
+/* Returns NULL if successful, otherwise a malloc()'d string describing the
+ * error.  The caller is responsible for freeing the returned string. */
+static char * WARN_UNUSED_RESULT
+learn_parse__(char *orig, char *arg, struct ofpbuf *ofpacts)
 {
-    char *orig = xstrdup(arg);
-    char *name, *value;
-
     struct ofpact_learn *learn;
-    struct cls_rule rule;
-    enum ofperr error;
+    struct match match;
+    char *name, *value;
 
     learn = ofpact_put_LEARN(ofpacts);
     learn->idle_timeout = OFP_FLOW_PERMANENT;
@@ -529,13 +560,13 @@ learn_parse(char *arg, const struct flow *flow, struct ofpbuf *ofpacts)
     learn->priority = OFP_DEFAULT_PRIORITY;
     learn->table_id = 1;
 
-    cls_rule_init_catchall(&rule, 0);
+    match_init_catchall(&match);
     while (ofputil_parse_key_value(&arg, &name, &value)) {
         if (!strcmp(name, "table")) {
             learn->table_id = atoi(value);
             if (learn->table_id == 255) {
-                ovs_fatal(0, "%s: table id 255 not valid for `learn' action",
-                          orig);
+                return xasprintf("%s: table id 255 not valid for `learn' "
+                                 "action", orig);
             }
         } else if (!strcmp(name, "priority")) {
             learn->priority = atoi(value);
@@ -551,63 +582,49 @@ learn_parse(char *arg, const struct flow *flow, struct ofpbuf *ofpacts)
             learn->cookie = strtoull(value, NULL, 0);
         } else {
             struct ofpact_learn_spec *spec;
+            char *error;
 
             spec = ofpbuf_put_zeros(ofpacts, sizeof *spec);
-            learn = ofpacts->l2;
+            learn = ofpacts->frame;
             learn->n_specs++;
 
-            learn_parse_spec(orig, name, value, spec);
-
-            /* Check prerequisites. */
-            if (spec->src_type == NX_LEARN_SRC_FIELD
-                && flow && !mf_are_prereqs_ok(spec->src.field, flow)) {
-                ovs_fatal(0, "%s: cannot specify source field %s because "
-                          "prerequisites are not satisfied",
-                          orig, spec->src.field->name);
-            }
-            if ((spec->dst_type == NX_LEARN_DST_MATCH
-                 || spec->dst_type == NX_LEARN_DST_LOAD)
-                && !mf_are_prereqs_ok(spec->dst.field, &rule.flow)) {
-                ovs_fatal(0, "%s: cannot specify destination field %s because "
-                          "prerequisites are not satisfied",
-                          orig, spec->dst.field->name);
+            error = learn_parse_spec(orig, name, value, spec);
+            if (error) {
+                return error;
             }
 
-            /* Update 'rule' to allow for satisfying destination
+            /* Update 'match' to allow for satisfying destination
              * prerequisites. */
             if (spec->src_type == NX_LEARN_SRC_IMMEDIATE
                 && spec->dst_type == NX_LEARN_DST_MATCH) {
-                mf_write_subfield(&spec->dst, &spec->src_imm, &rule);
+                mf_write_subfield(&spec->dst, &spec->src_imm, &match);
             }
         }
     }
     ofpact_update_len(ofpacts, &learn->ofpact);
 
-    /* In theory the above should have caught any errors, but... */
-    if (flow) {
-        error = learn_check(learn, flow);
-        if (error) {
-            ovs_fatal(0, "%s: %s", orig, ofperr_to_string(error));
-        }
-    }
-    free(orig);
+    return NULL;
 }
 
-static void
-format_subvalue(const union mf_subvalue *subvalue, struct ds *s)
+/* Parses 'arg' as a set of arguments to the "learn" action and appends a
+ * matching OFPACT_LEARN action to 'ofpacts'.  ovs-ofctl(8) describes the
+ * format parsed.
+ *
+ * Returns NULL if successful, otherwise a malloc()'d string describing the
+ * error.  The caller is responsible for freeing the returned string.
+ *
+ * If 'flow' is nonnull, then it should be the flow from a struct match that is
+ * the matching rule for the learning action.  This helps to better validate
+ * the action's arguments.
+ *
+ * Modifies 'arg'. */
+char * WARN_UNUSED_RESULT
+learn_parse(char *arg, struct ofpbuf *ofpacts)
 {
-    int i;
-
-    for (i = 0; i < ARRAY_SIZE(subvalue->u8); i++) {
-        if (subvalue->u8[i]) {
-            ds_put_format(s, "0x%"PRIx8, subvalue->u8[i]);
-            for (i++; i < ARRAY_SIZE(subvalue->u8); i++) {
-                ds_put_format(s, "%02"PRIx8, subvalue->u8[i]);
-            }
-            return;
-        }
-    }
-    ds_put_char(s, '0');
+    char *orig = xstrdup(arg);
+    char *error = learn_parse__(orig, arg, ofpacts);
+    free(orig);
+    return error;
 }
 
 /* Appends a description of 'learn' to 's', in the format that ovs-ofctl(8)
@@ -616,9 +633,9 @@ void
 learn_format(const struct ofpact_learn *learn, struct ds *s)
 {
     const struct ofpact_learn_spec *spec;
-    struct cls_rule rule;
+    struct match match;
 
-    cls_rule_init_catchall(&rule, 0);
+    match_init_catchall(&match);
 
     ds_put_format(s, "learn(table=%"PRIu8, learn->table_id);
     if (learn->idle_timeout != OFP_FLOW_PERMANENT) {
@@ -661,7 +678,7 @@ learn_format(const struct ofpact_learn *learn, struct ds *s)
             } else {
                 mf_format_subfield(&spec->dst, s);
                 ds_put_char(s, '=');
-                format_subvalue(&spec->src_imm, s);
+                mf_format_subvalue(&spec->src_imm, s);
             }
             break;
 
@@ -676,7 +693,7 @@ learn_format(const struct ofpact_learn *learn, struct ds *s)
 
         case NX_LEARN_SRC_IMMEDIATE | NX_LEARN_DST_LOAD:
             ds_put_format(s, "load:");
-            format_subvalue(&spec->src_imm, s);
+            mf_format_subvalue(&spec->src_imm, s);
             ds_put_cstr(s, "->");
             mf_format_subfield(&spec->dst, s);
             break;