Move Autoconf's macro definitions into config.h.
[openvswitch] / switch / switch-flow.c
index cacf690a3a391ed4b30b437a63e75106f9d7c5d9..732cea540d239b0682deab2a90f1211c317f1f0e 100644 (file)
@@ -1,24 +1,37 @@
-/* Copyright (C) 2008 Board of Trustees, Leland Stanford Jr. University.
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to
- * deal in the Software without restriction, including without limitation the
- * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
- * sell copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
- * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
- * IN THE SOFTWARE.
+/* Copyright (c) 2008 The Board of Trustees of The Leland Stanford
+ * Junior University
+ * 
+ * We are making the OpenFlow specification and associated documentation
+ * (Software) available for public use and benefit with the expectation
+ * that others will use, modify and enhance the Software and contribute
+ * those enhancements back to the community. However, since we would
+ * like to make the Software available for broadest use, with as few
+ * restrictions as possible permission is hereby granted, free of
+ * charge, to any person obtaining a copy of this Software to deal in
+ * the Software under the copyrights without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ * 
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ * 
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ * 
+ * The name and trademarks of copyright holder(s) may NOT be used in
+ * advertising or publicity pertaining to the Software or any
+ * derivatives without specific, written prior permission.
  */
 
+#include <config.h>
 #include "switch-flow.h"
 #include <arpa/inet.h>
 #include <assert.h>
@@ -69,17 +82,47 @@ int flow_del_matches(const struct sw_flow_key *t, const struct sw_flow_key *d, i
 void flow_extract_match(struct sw_flow_key* to, const struct ofp_match* from)
 {
     to->wildcards = ntohs(from->wildcards) & OFPFW_ALL;
-    to->flow.in_port   = from->in_port;
-    to->flow.dl_vlan   = from->dl_vlan;
+    to->flow.reserved = 0;
+    to->flow.in_port = from->in_port;
+    to->flow.dl_vlan = from->dl_vlan;
     memcpy(to->flow.dl_src, from->dl_src, ETH_ADDR_LEN);
     memcpy(to->flow.dl_dst, from->dl_dst, ETH_ADDR_LEN);
-    to->flow.dl_type   = from->dl_type;
-    to->flow.nw_src   = from->nw_src;
-    to->flow.nw_dst   = from->nw_dst;
-    to->flow.nw_proto  = from->nw_proto;
-    to->flow.tp_src   = from->tp_src;
-    to->flow.tp_dst   = from->tp_dst;
-    to->flow.reserved = 0;
+    to->flow.dl_type = from->dl_type;
+
+    to->flow.nw_src = to->flow.nw_dst = to->flow.nw_proto = 0;
+    to->flow.tp_src = to->flow.tp_dst = 0;
+
+#define OFPFW_TP (OFPFW_TP_SRC | OFPFW_TP_DST)
+#define OFPFW_NW (OFPFW_NW_SRC | OFPFW_NW_DST | OFPFW_NW_PROTO)
+    if (to->wildcards & OFPFW_DL_TYPE) {
+        /* Can't sensibly match on network or transport headers if the
+         * data link type is unknown. */
+        to->wildcards |= OFPFW_NW | OFPFW_TP;
+    } else if (from->dl_type == htons(ETH_TYPE_IP)) {
+        to->flow.nw_src   = from->nw_src;
+        to->flow.nw_dst   = from->nw_dst;
+        to->flow.nw_proto = from->nw_proto;
+
+        if (to->wildcards & OFPFW_NW_PROTO) {
+            /* Can't sensibly match on transport headers if the network
+             * protocol is unknown. */
+            to->wildcards |= OFPFW_TP;
+        } else if (from->nw_proto == IPPROTO_TCP 
+                || from->nw_proto == IPPROTO_UDP) {
+            to->flow.tp_src = from->tp_src;
+            to->flow.tp_dst = from->tp_dst;
+        } else {
+            /* Transport layer fields are undefined.  Mark them as
+             * exact-match to allow such flows to reside in table-hash,
+             * instead of falling into table-linear. */
+            to->wildcards &= ~OFPFW_TP;
+        }
+    } else {
+        /* Network and transport layer fields are undefined.  Mark them
+         * as exact-match to allow such flows to reside in table-hash,
+         * instead of falling into table-linear. */
+        to->wildcards &= ~(OFPFW_NW | OFPFW_TP);
+    }
 }
 
 void flow_fill_match(struct ofp_match* to, const struct sw_flow_key* from)