flow: Remove flow_to/from_match() in favor of cls_rule_to/from_match().
authorBen Pfaff <blp@nicira.com>
Mon, 22 Nov 2010 18:10:14 +0000 (10:10 -0800)
committerBen Pfaff <blp@nicira.com>
Mon, 22 Nov 2010 18:10:14 +0000 (10:10 -0800)
The flow_from_match() and flow_to_match() functions have to deal with most
of the state in a cls_rule anyhow, and this will increase in upcoming
commits, to the point that we might as well just use a cls_rule anyhow.
This commit therefore deletes flow_from_match() and flow_to_match(),
integrating their code into cls_rule_from_match() and the new function
cls_rule_to_match(), respectively.  It also changes each of the functions'
callers to use the new cls_rule_*() function.

lib/classifier.c
lib/classifier.h
lib/flow.c
lib/flow.h
lib/ofp-parse.c
ofproto/ofproto.c
tests/test-flows.c
utilities/ovs-ofctl.c

index c57c7e56408dc1bc4a23ebb95a902b0cafb9c415..76a33dd253f84671bcda7d577297ed56e6bbcf63 100644 (file)
@@ -19,6 +19,7 @@
 #include <assert.h>
 #include <errno.h>
 #include <netinet/in.h>
+#include "byte-order.h"
 #include "dynamic-string.h"
 #include "flow.h"
 #include "hash.h"
@@ -85,6 +86,16 @@ cls_rule_init_exact(const struct flow *flow,
     rule->priority = priority;
 }
 
+/* Initializes 'rule' as a "catch-all" rule that matches every packet, with
+ * priority 'priority'. */
+void
+cls_rule_init_catchall(struct cls_rule *rule, unsigned int priority)
+{
+    memset(&rule->flow, 0, sizeof rule->flow);
+    flow_wildcards_init(&rule->wc, OVSFW_ALL | FWW_ALL);
+    rule->priority = priority;
+}
+
 /* Converts the ofp_match in 'match' (with format 'flow_format', one of NXFF_*)
  * into a cls_rule in 'rule', with the given 'priority'.  'cookie' is used
  * when 'flow_format' is NXFF_TUN_ID_FROM_COOKIE. */
@@ -93,19 +104,67 @@ cls_rule_from_match(const struct ofp_match *match, unsigned int priority,
                     int flow_format, uint64_t cookie,
                     struct cls_rule *rule)
 {
-    flow_from_match(match, flow_format, cookie, &rule->flow, &rule->wc);
-    rule->priority = !rule->wc.wildcards ? UINT16_MAX : priority;
+    uint32_t wildcards = ntohl(match->wildcards) & OVSFW_ALL;
+
+    rule->priority = !wildcards ? UINT16_MAX : priority;
+
+    rule->flow.tun_id = 0;
+    if (flow_format != NXFF_TUN_ID_FROM_COOKIE) {
+        wildcards |= NXFW_TUN_ID;
+    } else {
+        if (!(wildcards & NXFW_TUN_ID)) {
+            rule->flow.tun_id = htonl(ntohll(cookie) >> 32);
+        }
+    }
+    if (wildcards & OFPFW_DL_DST) {
+        /* OpenFlow 1.0 OFPFW_DL_DST covers the whole Ethernet destination, but
+         * internally to OVS it excludes the multicast bit, which has to be set
+         * separately with FWW_ETH_MCAST. */
+        wildcards |= FWW_ETH_MCAST;
+    }
+    flow_wildcards_init(&rule->wc, wildcards);
+
+    rule->flow.nw_src = match->nw_src;
+    rule->flow.nw_dst = match->nw_dst;
+    rule->flow.in_port = (match->in_port == htons(OFPP_LOCAL) ? ODPP_LOCAL
+                     : ntohs(match->in_port));
+    rule->flow.dl_vlan = match->dl_vlan;
+    rule->flow.dl_vlan_pcp = match->dl_vlan_pcp;
+    rule->flow.dl_type = match->dl_type;
+    rule->flow.tp_src = match->tp_src;
+    rule->flow.tp_dst = match->tp_dst;
+    memcpy(rule->flow.dl_src, match->dl_src, ETH_ADDR_LEN);
+    memcpy(rule->flow.dl_dst, match->dl_dst, ETH_ADDR_LEN);
+    rule->flow.nw_tos = match->nw_tos;
+    rule->flow.nw_proto = match->nw_proto;
+
     cls_rule_zero_wildcarded_fields(rule);
 }
 
-/* Initializes 'rule' as a "catch-all" rule that matches every packet, with
- * priority 'priority'. */
+/* Converts 'rule' into an OpenFlow match structure 'match' with the given flow
+ * format 'flow_format' (one of NXFF_*). */
 void
-cls_rule_init_catchall(struct cls_rule *rule, unsigned int priority)
-{
-    memset(&rule->flow, 0, sizeof rule->flow);
-    flow_wildcards_init(&rule->wc, OVSFW_ALL | FWW_ALL);
-    rule->priority = priority;
+cls_rule_to_match(const struct cls_rule *rule, int flow_format,
+                  struct ofp_match *match)
+{
+    match->wildcards = htonl(rule->wc.wildcards
+                             & (flow_format == NXFF_TUN_ID_FROM_COOKIE
+                                ? OVSFW_ALL : OFPFW_ALL));
+    match->in_port = htons(rule->flow.in_port == ODPP_LOCAL ? OFPP_LOCAL
+                           : rule->flow.in_port);
+    match->dl_vlan = rule->flow.dl_vlan;
+    match->dl_vlan_pcp = rule->flow.dl_vlan_pcp;
+    memcpy(match->dl_src, rule->flow.dl_src, ETH_ADDR_LEN);
+    memcpy(match->dl_dst, rule->flow.dl_dst, ETH_ADDR_LEN);
+    match->dl_type = rule->flow.dl_type;
+    match->nw_src = rule->flow.nw_src;
+    match->nw_dst = rule->flow.nw_dst;
+    match->nw_tos = rule->flow.nw_tos;
+    match->nw_proto = rule->flow.nw_proto;
+    match->tp_src = rule->flow.tp_src;
+    match->tp_dst = rule->flow.tp_dst;
+    memset(match->pad1, '\0', sizeof match->pad1);
+    memset(match->pad2, '\0', sizeof match->pad2);
 }
 
 /* For each bit or field wildcarded in 'rule', sets the corresponding bit or
index ea4d6fc8332dd3755261a8071539a06f3f516940..d8135e9ec0973c52f65babe6cbb06005e8c82398 100644 (file)
@@ -70,9 +70,12 @@ void cls_rule_init(const struct flow *, const struct flow_wildcards *,
                    unsigned int priority, struct cls_rule *);
 void cls_rule_init_exact(const struct flow *, unsigned int priority,
                          struct cls_rule *);
+void cls_rule_init_catchall(struct cls_rule *, unsigned int priority);
+
 void cls_rule_from_match(const struct ofp_match *, unsigned int priority,
                          int flow_format, uint64_t cookie, struct cls_rule *);
-void cls_rule_init_catchall(struct cls_rule *, unsigned int priority);
+void cls_rule_to_match(const struct cls_rule *, int flow_format,
+                       struct ofp_match *);
 
 void cls_rule_zero_wildcarded_fields(struct cls_rule *);
 
index a59958544ecfbab82f711c29b94f64301bf6470a..7c3ad5118fb5c21d02d3988f2af6447dc5cbfb9d 100644 (file)
@@ -250,71 +250,6 @@ flow_extract_stats(const struct flow *flow, struct ofpbuf *packet,
     stats->n_packets = 1;
 }
 
-/* Extract 'flow' with 'wildcards' into the OpenFlow match structure
- * 'match'.  'flow_format' should be one of NXFF_*. */
-void
-flow_to_match(const struct flow *flow, uint32_t wildcards,
-              int flow_format, struct ofp_match *match)
-{
-    wildcards &= (flow_format == NXFF_TUN_ID_FROM_COOKIE ? OVSFW_ALL
-                  : OFPFW_ALL);
-    match->wildcards = htonl(wildcards);
-
-    match->in_port = htons(flow->in_port == ODPP_LOCAL ? OFPP_LOCAL
-                           : flow->in_port);
-    match->dl_vlan = flow->dl_vlan;
-    match->dl_vlan_pcp = flow->dl_vlan_pcp;
-    memcpy(match->dl_src, flow->dl_src, ETH_ADDR_LEN);
-    memcpy(match->dl_dst, flow->dl_dst, ETH_ADDR_LEN);
-    match->dl_type = flow->dl_type;
-    match->nw_src = flow->nw_src;
-    match->nw_dst = flow->nw_dst;
-    match->nw_tos = flow->nw_tos;
-    match->nw_proto = flow->nw_proto;
-    match->tp_src = flow->tp_src;
-    match->tp_dst = flow->tp_dst;
-    memset(match->pad1, '\0', sizeof match->pad1);
-    memset(match->pad2, '\0', sizeof match->pad2);
-}
-
-void
-flow_from_match(const struct ofp_match *match, int flow_format,
-                ovs_be64 cookie, struct flow *flow,
-                struct flow_wildcards *wc)
-{
-    uint32_t wildcards = ntohl(match->wildcards) & OVSFW_ALL;
-
-    flow->tun_id = 0;
-    if (flow_format != NXFF_TUN_ID_FROM_COOKIE) {
-        wildcards |= NXFW_TUN_ID;
-    } else {
-        if (!(wildcards & NXFW_TUN_ID)) {
-            flow->tun_id = htonl(ntohll(cookie) >> 32);
-        }
-    }
-    if (wildcards & OFPFW_DL_DST) {
-        /* OpenFlow 1.0 OFPFW_DL_DST covers the whole Ethernet destination, but
-         * internally to OVS it excludes the multicast bit, which has to be set
-         * separately with FWW_ETH_MCAST. */
-        wildcards |= FWW_ETH_MCAST;
-    }
-    flow_wildcards_init(wc, wildcards);
-
-    flow->nw_src = match->nw_src;
-    flow->nw_dst = match->nw_dst;
-    flow->in_port = (match->in_port == htons(OFPP_LOCAL) ? ODPP_LOCAL
-                     : ntohs(match->in_port));
-    flow->dl_vlan = match->dl_vlan;
-    flow->dl_vlan_pcp = match->dl_vlan_pcp;
-    flow->dl_type = match->dl_type;
-    flow->tp_src = match->tp_src;
-    flow->tp_dst = match->tp_dst;
-    memcpy(flow->dl_src, match->dl_src, ETH_ADDR_LEN);
-    memcpy(flow->dl_dst, match->dl_dst, ETH_ADDR_LEN);
-    flow->nw_tos = match->nw_tos;
-    flow->nw_proto = match->nw_proto;
-}
-
 char *
 flow_to_string(const struct flow *flow)
 {
index 8292359e096305f4b60c0aeac3af009574470774..1a704bff94de0186ed07599809fd20edc2362e90 100644 (file)
@@ -64,10 +64,6 @@ int flow_extract(struct ofpbuf *, ovs_be32 tun_id, uint16_t in_port,
                  struct flow *);
 void flow_extract_stats(const struct flow *flow, struct ofpbuf *packet,
         struct odp_flow_stats *stats);
-void flow_to_match(const struct flow *, uint32_t wildcards, int flow_format,
-                   struct ofp_match *);
-void flow_from_match(const struct ofp_match *, int flow_format,
-                     ovs_be64 cookie, struct flow *, struct flow_wildcards *);
 char *flow_to_string(const struct flow *);
 void flow_format(struct ds *, const struct flow *);
 void flow_print(FILE *, const struct flow *);
index 20440323f7832730be65fcb4d10d629a88516aa5..d0488ddb3a9daab880e7f1e5c0a04989096d9c30 100644 (file)
@@ -608,8 +608,7 @@ parse_ofp_flow_mod_str(char *string, uint16_t command)
     parse_ofp_str(&pf, buffer, string);
 
     ofm = buffer->data;
-    flow_to_match(&pf.rule.flow, pf.rule.wc.wildcards, NXFF_OPENFLOW10,
-                  &ofm->match);
+    cls_rule_to_match(&pf.rule, NXFF_OPENFLOW10, &ofm->match);
     ofm->command = htons(command);
     ofm->cookie = htonll(pf.cookie);
     ofm->idle_timeout = htons(pf.idle_timeout);
index a8fc28b657cf56c521756341190868ffdf9b9f98..bf4a51fcafa6a98cb326c63c55f830fbe3115379 100644 (file)
@@ -3412,8 +3412,7 @@ put_ofp_flow_stats(struct ofconn *ofconn, struct rule *rule,
     ofs->length = htons(len);
     ofs->table_id = 0;
     ofs->pad = 0;
-    flow_to_match(&rule->cr.flow, rule->cr.wc.wildcards,
-                  ofconn->flow_format, &ofs->match);
+    cls_rule_to_match(&rule->cr, ofconn->flow_format, &ofs->match);
     calc_flow_duration(rule->created, &ofs->duration_sec, &ofs->duration_nsec);
     ofs->cookie = rule->flow_cookie;
     ofs->priority = htons(rule->cr.priority);
@@ -3544,8 +3543,7 @@ flow_stats_ds(struct ofproto *ofproto, struct rule *rule, struct ds *results)
     size_t act_len = sizeof *rule->actions * rule->n_actions;
 
     query_stats(ofproto, rule, &packet_count, &byte_count);
-    flow_to_match(&rule->cr.flow, rule->cr.wc.wildcards,
-                  NXFF_OPENFLOW10, &match);
+    cls_rule_to_match(&rule->cr, NXFF_OPENFLOW10, &match);
 
     ds_put_format(results, "duration=%llds, ",
                   (time_msec() - rule->created) / 1000);
@@ -4815,8 +4813,7 @@ compose_ofp_flow_removed(struct ofconn *ofconn, const struct rule *rule,
     struct ofpbuf *buf;
 
     ofr = make_openflow(sizeof *ofr, OFPT_FLOW_REMOVED, &buf);
-    flow_to_match(&rule->cr.flow, rule->cr.wc.wildcards, ofconn->flow_format,
-                  &ofr->match);
+    cls_rule_to_match(&rule->cr, ofconn->flow_format, &ofr->match);
     ofr->cookie = rule->flow_cookie;
     ofr->priority = htons(rule->cr.priority);
     ofr->reason = reason;
index a4649b3f81de1e38202eecaec5454b4cbebf465f..b53d8532c423403dcc3c1dad9cdfc9f0d2ffe62a 100644 (file)
 #include <errno.h>
 #include <stdlib.h>
 #include <string.h>
+#include "classifier.h"
 #include "openflow/openflow.h"
 #include "timeval.h"
 #include "ofpbuf.h"
 #include "ofp-print.h"
+#include "ofp-util.h"
 #include "pcap.h"
 #include "util.h"
 #include "vlog.h"
@@ -54,6 +56,7 @@ main(int argc OVS_UNUSED, char *argv[])
     while (fread(&expected_match, sizeof expected_match, 1, flows)) {
         struct ofpbuf *packet;
         struct ofp_match extracted_match;
+        struct cls_rule rule;
         struct flow flow;
 
         n++;
@@ -66,7 +69,8 @@ main(int argc OVS_UNUSED, char *argv[])
         }
 
         flow_extract(packet, 0, 1, &flow);
-        flow_to_match(&flow, 0, false, &extracted_match);
+        cls_rule_init_exact(&flow, 0, &rule);
+        cls_rule_to_match(&rule, NXFF_OPENFLOW10, &extracted_match);
 
         if (memcmp(&expected_match, &extracted_match, sizeof expected_match)) {
             char *exp_s = ofp_match_to_string(&expected_match, 2);
index be68bdcb1692eff78b58c56738e8663cbfcbfa67..3a0056ad188066efd3da73f9d9f154c0b54776c5 100644 (file)
@@ -443,8 +443,7 @@ do_dump_flows(int argc, char *argv[])
 
     req = alloc_stats_request(sizeof *req, OFPST_FLOW, &request);
     parse_ofp_str(&pf, NULL, argc > 2 ? argv[2] : "");
-    flow_to_match(&pf.rule.flow, pf.rule.wc.wildcards, NXFF_OPENFLOW10,
-                  &req->match);
+    cls_rule_to_match(&pf.rule, NXFF_OPENFLOW10, &req->match);
     memset(&req->pad, 0, sizeof req->pad);
     req->out_port = htons(pf.out_port);
 
@@ -460,8 +459,7 @@ do_dump_aggregate(int argc, char *argv[])
 
     req = alloc_stats_request(sizeof *req, OFPST_AGGREGATE, &request);
     parse_ofp_str(&pf, NULL, argc > 2 ? argv[2] : "");
-    flow_to_match(&pf.rule.flow, pf.rule.wc.wildcards, NXFF_OPENFLOW10,
-                  &req->match);
+    cls_rule_to_match(&pf.rule, NXFF_OPENFLOW10, &req->match);
     memset(&req->pad, 0, sizeof req->pad);
     req->out_port = htons(pf.out_port);