Generalize conversions between struct flow and struct ofp_match.
authorBen Pfaff <blp@nicira.com>
Mon, 2 Mar 2009 21:42:04 +0000 (13:42 -0800)
committerBen Pfaff <blp@nicira.com>
Mon, 2 Mar 2009 21:42:04 +0000 (13:42 -0800)
lib/flow.c
lib/flow.h
tests/test-flows.c
udatapath/datapath.c
vswitchd/bridge.c

index d627648cf2c7c7dbb580d222ea2d9db6853cf1b8..5fa79aa61d4cdd2a4ebaa38877552ec7ea21d5c1 100644 (file)
@@ -35,6 +35,7 @@
 #include "flow.h"
 #include <inttypes.h>
 #include <netinet/in.h>
+#include <stdlib.h>
 #include <string.h>
 #include "hash.h"
 #include "ofpbuf.h"
@@ -104,7 +105,7 @@ flow_extract(struct ofpbuf *packet, uint16_t in_port, struct flow *flow)
 
     memset(flow, 0, sizeof *flow);
     flow->dl_vlan = htons(OFP_VLAN_NONE);
-    flow->in_port = htons(in_port);
+    flow->in_port = in_port;
 
     packet->l2 = b.data;
     packet->l3 = NULL;
@@ -202,32 +203,58 @@ flow_extract(struct ofpbuf *packet, uint16_t in_port, struct flow *flow)
 }
 
 void
-flow_fill_match(struct ofp_match *to, const struct flow *from,
-                uint32_t wildcards)
+flow_to_match(const struct flow *flow, uint32_t wildcards, struct ofp_match *match)
 {
-    to->wildcards = htonl(wildcards);
-    to->in_port = from->in_port;
-    to->dl_vlan = from->dl_vlan;
-    memcpy(to->dl_src, from->dl_src, ETH_ADDR_LEN);
-    memcpy(to->dl_dst, from->dl_dst, ETH_ADDR_LEN);
-    to->dl_type = from->dl_type;
-    to->nw_src = from->nw_src;
-    to->nw_dst = from->nw_dst;
-    to->nw_proto = from->nw_proto;
-    to->tp_src = from->tp_src;
-    to->tp_dst = from->tp_dst;
-    to->pad = 0;
+    match->wildcards = htonl(wildcards);
+    match->in_port = htons(flow->in_port);
+    match->dl_vlan = flow->dl_vlan;
+    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_proto = flow->nw_proto;
+    match->tp_src = flow->tp_src;
+    match->tp_dst = flow->tp_dst;
+    match->pad = 0;
+}
+
+void
+flow_from_match(struct flow *flow, uint32_t *wildcards,
+                const struct ofp_match *match)
+{
+    if (wildcards) {
+        *wildcards = ntohl(match->wildcards);
+    }
+    flow->nw_src = match->nw_src;
+    flow->nw_dst = match->nw_dst;
+    flow->in_port = ntohs(match->in_port);
+    flow->dl_vlan = match->dl_vlan;
+    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_proto = match->nw_proto;
+    flow->reserved = 0;
+}
+
+char *
+flow_to_string(const struct flow *flow)
+{
+    return xasprintf("port%04x:vlan%d mac"ETH_ADDR_FMT"->"ETH_ADDR_FMT" "
+                     "type%04x proto%"PRId8" ip"IP_FMT"->"IP_FMT" port%d->%d",
+                     flow->in_port, ntohs(flow->dl_vlan),
+                     ETH_ADDR_ARGS(flow->dl_src), ETH_ADDR_ARGS(flow->dl_dst),
+                     ntohs(flow->dl_type), flow->nw_proto,
+                     IP_ARGS(&flow->nw_src), IP_ARGS(&flow->nw_dst),
+                     ntohs(flow->tp_src), ntohs(flow->tp_dst));
 }
 
 void
 flow_print(FILE *stream, const struct flow *flow) 
 {
-    fprintf(stream,
-            "port%04x:vlan%d mac"ETH_ADDR_FMT"->"ETH_ADDR_FMT" "
-            "type%04x proto%"PRId8" ip"IP_FMT"->"IP_FMT" port%d->%d",
-            ntohs(flow->in_port), ntohs(flow->dl_vlan),
-            ETH_ADDR_ARGS(flow->dl_src), ETH_ADDR_ARGS(flow->dl_dst),
-            ntohs(flow->dl_type), flow->nw_proto,
-            IP_ARGS(&flow->nw_src), IP_ARGS(&flow->nw_dst),
-            ntohs(flow->tp_src), ntohs(flow->tp_dst));
+    char *s = flow_to_string(flow);
+    fputs(s, stream);
+    free(s);
 }
index 367a0d3a3033968df3feca62ddd67e48a45e88db..bf3aaa88309b29d89c8f0288014e1d6d7c0f7d6c 100644 (file)
@@ -63,8 +63,11 @@ struct flow {
 BUILD_ASSERT_DECL(sizeof(struct flow) == 32);
 
 int flow_extract(struct ofpbuf *, uint16_t in_port, struct flow *);
-void flow_fill_match(struct ofp_match *, const struct flow *,
-                     uint32_t wildcards);
+void flow_to_match(const struct flow *, uint32_t wildcards,
+                   struct ofp_match *);
+void flow_from_match(struct flow *, uint32_t *wildcards,
+                     const struct ofp_match *);
+char *flow_to_string(const struct flow *);
 void flow_print(FILE *, const struct flow *);
 static inline int flow_compare(const struct flow *, const struct flow *);
 static inline bool flow_equal(const struct flow *, const struct flow *);
index fb4026fb96a076b1eced7dda067e16584d4e9b54..2bea3b336885b97f43c57f86bb2dce911d681afe 100644 (file)
@@ -52,7 +52,7 @@ main(int argc UNUSED, char *argv[])
         }
 
         flow_extract(packet, 0, &flow);
-        flow_fill_match(&extracted_match, &flow, 0);
+        flow_to_match(&flow, 0, &extracted_match);
 
         if (memcmp(&expected_match, &extracted_match, sizeof expected_match)) {
             char *exp_s = ofp_match_to_string(&expected_match, 2);
index 6324b192e54e39e2d74f4eb0d75e2cc611dce5b0..838f7b552f8deb4a81169bf4575410b446d0152e 100644 (file)
@@ -744,7 +744,7 @@ dp_send_flow_end(struct datapath *dp, struct sw_flow *flow,
     nfe->header.vendor = htonl(NX_VENDOR_ID);
     nfe->header.subtype = htonl(NXT_FLOW_END);
 
-    flow_fill_match(&nfe->match, &flow->key.flow, flow->key.wildcards);
+    flow_to_match(&flow->key.flow, flow->key.wildcards, &nfe->match);
 
     nfe->priority = htons(flow->priority);
     nfe->reason = reason;
index d42899819a0c206b04e52bff3c92e90d7fd25870..7ff6c66c3caf1b93b0943a1b458f8d948f76e88a 100644 (file)
@@ -1116,8 +1116,6 @@ static packet_handler_func process_error_msg;
 static packet_handler_func process_features_reply;
 static packet_handler_func process_port_status;
 
-static void flow_from_match(struct flow *, const struct ofp_match *);
-
 static void
 bridge_process_msg(struct bridge *br, struct ofpbuf *msg)
 {
@@ -1919,7 +1917,7 @@ process_flow_expired(struct bridge *br, void *ofe_)
         /* We don't use flows with wildcards, so there's nothing to do. */
         return;
     }
-    flow_from_match(&flow, &ofe->match);
+    flow_from_match(&flow, NULL, &ofe->match);
 
     f = ft_lookup(br->ft, &flow, flow_hash(&flow, 0));
     if (f) {
@@ -2008,22 +2006,6 @@ process_port_status(struct bridge *br, void *ops_)
     }
     phy_port_changed(br, ops->reason, &ops->desc);
 }
-
-static void
-flow_from_match(struct flow *flow, const struct ofp_match *match)
-{
-    flow->nw_src = match->nw_src;
-    flow->nw_dst = match->nw_dst;
-    flow->in_port = match->in_port;
-    flow->dl_vlan = match->dl_vlan;
-    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_proto = match->nw_proto;
-    flow->reserved = 0;
-}
 \f
 /* Flow statistics collection. */
 
@@ -2266,7 +2248,7 @@ flowstats_process(struct bridge *br)
                 /* XXX delete flow */
                 continue;
             }
-            flow_from_match(&flow, &fs->match);
+            flow_from_match(&flow, NULL, &fs->match);
             hash = flow_hash(&flow, 0);
 
             f = ft_lookup(br->ft, &flow, hash);