From 9740dcc91ec0afd3532778a6400649e4e8df90ab Mon Sep 17 00:00:00 2001 From: Ben Pfaff Date: Mon, 2 Mar 2009 13:42:04 -0800 Subject: [PATCH] Generalize conversions between struct flow and struct ofp_match. --- lib/flow.c | 73 ++++++++++++++++++++++++++++++-------------- lib/flow.h | 7 +++-- tests/test-flows.c | 2 +- udatapath/datapath.c | 2 +- vswitchd/bridge.c | 22 ++----------- 5 files changed, 59 insertions(+), 47 deletions(-) diff --git a/lib/flow.c b/lib/flow.c index d627648c..5fa79aa6 100644 --- a/lib/flow.c +++ b/lib/flow.c @@ -35,6 +35,7 @@ #include "flow.h" #include #include +#include #include #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); } diff --git a/lib/flow.h b/lib/flow.h index 367a0d3a..bf3aaa88 100644 --- a/lib/flow.h +++ b/lib/flow.h @@ -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 *); diff --git a/tests/test-flows.c b/tests/test-flows.c index fb4026fb..2bea3b33 100644 --- a/tests/test-flows.c +++ b/tests/test-flows.c @@ -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); diff --git a/udatapath/datapath.c b/udatapath/datapath.c index 6324b192..838f7b55 100644 --- a/udatapath/datapath.c +++ b/udatapath/datapath.c @@ -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; diff --git a/vswitchd/bridge.c b/vswitchd/bridge.c index d4289981..7ff6c66c 100644 --- a/vswitchd/bridge.c +++ b/vswitchd/bridge.c @@ -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; -} /* 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); -- 2.30.2