From: Ben Pfaff Date: Tue, 27 Jan 2009 01:18:25 +0000 (-0800) Subject: Export network address mask logic in switch-flow.c for public use. X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=e53f0b3a0323005f28f745469262adf275d9d7a2;p=openvswitch Export network address mask logic in switch-flow.c for public use. The flow classifier needs to do the same kinds of tests. --- diff --git a/lib/flow.h b/lib/flow.h index 5c6c71d7..367a0d3a 100644 --- a/lib/flow.h +++ b/lib/flow.h @@ -33,6 +33,7 @@ #ifndef FLOW_H #define FLOW_H 1 +#include #include #include #include @@ -89,4 +90,23 @@ flow_hash(const struct flow *flow, uint32_t basis) sizeof *flow / sizeof(uint32_t), basis); } +/* Given the wildcard bit count in bits 'shift' through 'shift + 5' (inclusive) + * of 'wildcards', returns a 32-bit bit mask with a 1 in each bit that must + * match and a 0 in each bit that is wildcarded. + * + * The bits in 'wildcards' are in the format used in enum ofp_flow_wildcards: 0 + * is exact match, 1 ignores the LSB, 2 ignores the 2 least-significant bits, + * ..., 32 and higher wildcard the entire field. This is the *opposite* of the + * usual convention where e.g. /24 indicates that 8 bits (not 24 bits) are + * wildcarded. + * + * 'wildcards' is in host byte order. The return value is in network byte + * order. */ +static inline uint32_t +flow_nw_bits_to_mask(uint32_t wildcards, int shift) +{ + wildcards = (wildcards >> shift) & 0x3f; + return wildcards < 32 ? htonl(~((1u << wildcards) - 1)) : 0; +} + #endif /* flow.h */ diff --git a/udatapath/switch-flow.c b/udatapath/switch-flow.c index 6bc2d3c5..79c7eff5 100644 --- a/udatapath/switch-flow.c +++ b/udatapath/switch-flow.c @@ -60,12 +60,6 @@ flow_fields_match(const struct flow *a, const struct flow *b, uint16_t w, && (w & OFPFW_TP_DST || a->tp_dst == b->tp_dst)); } -static uint32_t make_nw_mask(int n_wild_bits) -{ - n_wild_bits &= (1u << OFPFW_NW_SRC_BITS) - 1; - return n_wild_bits < 32 ? htonl(~((1u << n_wild_bits) - 1)) : 0; -} - /* Returns nonzero if 'a' and 'b' match, that is, if their fields are equal * modulo wildcards in 'b', zero otherwise. */ inline int @@ -148,8 +142,8 @@ flow_extract_match(struct sw_flow_key* to, const struct ofp_match* from) } /* We set these late because code above adjusts to->wildcards. */ - to->nw_src_mask = make_nw_mask(to->wildcards >> OFPFW_NW_SRC_SHIFT); - to->nw_dst_mask = make_nw_mask(to->wildcards >> OFPFW_NW_DST_SHIFT); + to->nw_src_mask = flow_nw_bits_to_mask(to->wildcards, OFPFW_NW_SRC_SHIFT); + to->nw_dst_mask = flow_nw_bits_to_mask(to->wildcards, OFPFW_NW_DST_SHIFT); } /* Allocates and returns a new flow with room for 'actions_len' actions.