[ovs-discuss] Can anyone familiar with the following code please clarify for me?

YIMIN CHEN ymchen.nbzj at gmail.com
Tue May 22 06:11:35 UTC 2012


Hi,

I am working on ovs code based on openvswitch-1.1.0pre2, it is kinda
of old, I know. I ran across this code that I am having hard time
understanding. If anyone is familiar with this part of code, could you
please help me?


/* Compares the specified field in 'a' and 'b'.  Returns true if the fields are
 * equal, or if the ofp_match wildcard bits in 'wildcards' are set such that
 * non-equal values may be ignored.  'nw_src_mask' and 'nw_dst_mask' must be
 * those that would be set for 'wildcards' by cls_rule_set_masks().
 *
 * The compared field is the one with wildcard bit or bits 'field_wc', offset
 * 'rule_ofs' within cls_rule's "fields" member, and length 'len', in bytes. */
static inline bool ALWAYS_INLINE
field_matches(const flow_t *a_, const flow_t *b_,
             uint32_t wildcards, uint32_t nw_src_mask, uint32_t nw_dst_mask,
             uint32_t field_wc, int ofs, int len)
{
   /* This function is always inlined, and it is always called with 'field_wc'
    * as a compile-time constant, so the "if" conditionals here generate no
    * code. */
   const void *a = (const uint8_t *) a_ + ofs;
   const void *b = (const uint8_t *) b_ + ofs;
   if (!(field_wc & (field_wc - 1))) { <================== if field_wc
is zero or if field_wc is 1? what does it mean field_wc is 1?
       /* Handle all the single-bit wildcard cases. */
       return wildcards & field_wc || equal_bytes(a, b, len);
<===========1) based on operator precedence, we only compare a and b
if wildcards bit is zero or field_wc is zero? Shouldn't wildcards
being zero means we don't need need to compare? 2) what is the reason
for checking field_wc here?

   } else if (field_wc == OFPFW_NW_SRC_MASK ||
              field_wc == OFPFW_NW_DST_MASK) {
       uint32_t a_ip = read_uint32(a);
       uint32_t b_ip = read_uint32(b);
       uint32_t mask = (field_wc == OFPFW_NW_SRC_MASK
                        ? nw_src_mask : nw_dst_mask);
       return ((a_ip ^ b_ip) & mask) == 0;
   } else {
       abort();
   }
}


I really appreciate your clarification!


Thanks!
Yimin



More information about the discuss mailing list