ovs-ofctl: Accept port keywords, OF1.1 port numbers, reject port number 0.
[sliver-openvswitch.git] / lib / autopath.c
index 889b037..b204e84 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2011 Nicira Networks.
+ * Copyright (c) 2011, 2012 Nicira, Inc.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
 #include <stdlib.h>
 
 #include "flow.h"
+#include "meta-flow.h"
 #include "nx-match.h"
+#include "ofp-actions.h"
+#include "ofp-errors.h"
 #include "ofp-util.h"
 #include "openflow/nicira-ext.h"
 #include "vlog.h"
 
 VLOG_DEFINE_THIS_MODULE(autopath);
 
-static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
-
-/* Loads 'ofp_port' into the appropriate register in accordance with the
- * autopath action. */
-void
-autopath_execute(const struct nx_action_autopath *ap, struct flow *flow,
-                 uint16_t ofp_port)
-{
-    uint32_t *reg = &flow->regs[NXM_NX_REG_IDX(ntohl(ap->dst))];
-    int ofs = nxm_decode_ofs(ap->ofs_nbits);
-    int n_bits = nxm_decode_n_bits(ap->ofs_nbits);
-    uint32_t mask = n_bits == 32 ? UINT32_MAX : (UINT32_C(1) << n_bits) - 1;
-    *reg = (*reg & ~(mask << ofs)) | ((ofp_port & mask) << ofs);
-}
-
 void
-autopath_parse(struct nx_action_autopath *ap, const char *s_)
+autopath_parse(struct ofpact_autopath *ap, const char *s_)
 {
     char *s;
-    uint32_t reg;
-    int id_int, ofs, n_bits;
     char *id_str, *dst, *save_ptr;
 
+    ofpact_init_AUTOPATH(ap);
+
     s = xstrdup(s_);
     save_ptr = NULL;
     id_str = strtok_r(s, ", ", &save_ptr);
@@ -61,50 +49,58 @@ autopath_parse(struct nx_action_autopath *ap, const char *s_)
         ovs_fatal(0, "%s: not enough arguments to autopath action", s_);
     }
 
-    id_int = atoi(id_str);
-    if (id_int < 1 || id_int > UINT32_MAX) {
-        ovs_fatal(0, "%s: autopath id %d is not in valid range "
-                  "1 to %"PRIu32, s_, id_int, UINT32_MAX);
-    }
-
-    nxm_parse_field_bits(dst, &reg, &ofs, &n_bits);
-    if (!NXM_IS_NX_REG(reg) || NXM_NX_REG_IDX(reg) >= FLOW_N_REGS) {
-        ovs_fatal(0, "%s: destination field must be a register", s_);
+    ap->port = ofputil_port_from_string(id_str);
+    if (!ap->port) {
+        ovs_fatal(0, "%s: bad port number", s_);
     }
 
-    if (n_bits < 16) {
+    mf_parse_subfield(&ap->dst, dst);
+    if (ap->dst.n_bits < 16) {
         ovs_fatal(0, "%s: %d-bit destination field has %u possible values, "
-                  "less than required 65536", s_, n_bits, 1u << n_bits);
+                  "less than required 65536",
+                  s_, ap->dst.n_bits, 1u << ap->dst.n_bits);
     }
 
-    memset(ap, 0, sizeof *ap);
-    ap->type = htons(OFPAT_VENDOR);
-    ap->len = htons(sizeof *ap);
-    ap->vendor = htonl(NX_VENDOR_ID);
-    ap->subtype = htons(NXAST_AUTOPATH);
-    ap->id = htonl(id_int);
-    ap->ofs_nbits = nxm_encode_ofs_nbits(ofs, n_bits);
-    ap->dst = htonl(reg);
-
     free(s);
 }
 
-int
-autopath_check(const struct nx_action_autopath *ap)
+enum ofperr
+autopath_from_openflow(const struct nx_action_autopath *nap,
+                       struct ofpact_autopath *autopath)
 {
-    uint32_t dst = ntohl(ap->dst);
-    int ofs = nxm_decode_ofs(ap->ofs_nbits);
-    int n_bits = nxm_decode_n_bits(ap->ofs_nbits);
-
-    if (!NXM_IS_NX_REG(dst) || NXM_NX_REG_IDX(dst) >= FLOW_N_REGS) {
-        VLOG_WARN_RL(&rl, "unsupported destination field %#"PRIx32, dst);
-    } else if (ofs + n_bits > nxm_field_bits(dst)) {
-        VLOG_WARN_RL(&rl, "destination overflows output field");
-    } else if (n_bits < 16) {
-        VLOG_WARN_RL(&rl, "minimum of 16 bits required in output field");
-    } else {
-        return 0;
+    ofpact_init_AUTOPATH(autopath);
+    autopath->dst.field = mf_from_nxm_header(ntohl(nap->dst));
+    autopath->dst.ofs = nxm_decode_ofs(nap->ofs_nbits);
+    autopath->dst.n_bits = nxm_decode_n_bits(nap->ofs_nbits);
+    autopath->port = ntohl(nap->id);
+
+    if (autopath->dst.n_bits < 16) {
+        VLOG_WARN("at least 16 bit destination is required for autopath "
+                  "action.");
+        return OFPERR_OFPBAC_BAD_ARGUMENT;
     }
 
-    return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_ARGUMENT);
+    return autopath_check(autopath, NULL);
+}
+
+enum ofperr
+autopath_check(const struct ofpact_autopath *autopath, const struct flow *flow)
+{
+    VLOG_WARN_ONCE("The autopath action is deprecated and may be removed in"
+                   " February 2013.  Please email dev@openvswitch.org with"
+                   " concerns.");
+    return mf_check_dst(&autopath->dst, flow);
+}
+
+void
+autopath_to_nxast(const struct ofpact_autopath *autopath,
+                  struct ofpbuf *openflow)
+{
+    struct nx_action_autopath *ap;
+
+    ap = ofputil_put_NXAST_AUTOPATH__DEPRECATED(openflow);
+    ap->ofs_nbits = nxm_encode_ofs_nbits(autopath->dst.ofs,
+                                         autopath->dst.n_bits);
+    ap->dst = htonl(autopath->dst.field->nxm_header);
+    ap->id = htonl(autopath->port);
 }