Introduce helper function for mac address,
and simplify set_dl_{src, dst}_mask()Signed-off-by: Isaku Yamahata <[email protected]> --- ryu/lib/mac.py | 12 ++++++++++-- ryu/ofproto/nx_match.py | 8 ++------ 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/ryu/lib/mac.py b/ryu/lib/mac.py index 14218d3..2082643 100644 --- a/ryu/lib/mac.py +++ b/ryu/lib/mac.py @@ -14,6 +14,9 @@ # See the License for the specific language governing permissions and # limitations under the License. +import itertools + + # Internal representation of mac address is string[6] _HADDR_LEN = 6 @@ -31,7 +34,7 @@ def haddr_to_str(addr): """Format mac address in internal representation into human readable form""" assert len(addr) == _HADDR_LEN - return ':'.join(['%02x' % ord(char) for char in addr]) + return ':'.join('%02x' % ord(char) for char in addr) def haddr_to_bin(string): @@ -40,4 +43,9 @@ def haddr_to_bin(string): hexes = string.split(':') if len(hexes) != _HADDR_LEN: ValueError('Invalid format for mac address: %s' % string) - return ''.join([chr(int(h, 16)) for h in hexes]) + return ''.join(chr(int(h, 16)) for h in hexes) + + +def haddr_bitand(addr, mask): + return ''.join(chr(ord(a) & ord(m)) for (a, m) + in itertools.izip(addr, mask)) diff --git a/ryu/ofproto/nx_match.py b/ryu/ofproto/nx_match.py index 8192251..7a8bca7 100644 --- a/ryu/ofproto/nx_match.py +++ b/ryu/ofproto/nx_match.py @@ -109,18 +109,14 @@ class ClsRule(object): def set_dl_dst_masked(self, dl_dst, mask): self.wc.dl_dst_mask = mask # bit-wise and of the corresponding elements of dl_dst and mask - self.flow.dl_dst = reduce(lambda x, y: x + y, - map(lambda x: chr(ord(x[0]) & ord(x[1])), - zip(dl_dst, mask))) + self.flow.dl_dst = mac.haddr_bitand(dl_dst, mask) def set_dl_src(self, dl_src): self.flow.dl_src = dl_src def set_dl_src_masked(self, dl_src, mask): self.wc.dl_src_mask = mask - self.flow.dl_src = reduce(lambda x, y: x + y, - map(lambda x: chr(ord(x[0]) & ord(x[1])), - zip(dl_src, mask))) + self.flow.dl_src = mac.haddr_bitand(dl_src, mask) def set_dl_type(self, dl_type): self.wc.wildcards &= ~FWW_DL_TYPE -- 1.7.1.1 ------------------------------------------------------------------------------ Live Security Virtual Conference Exclusive live event will cover all the ways today's security and threat landscape has changed and how IT managers can respond. Discussions will include endpoint security, mobile security and the latest in malware threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/ _______________________________________________ Ryu-devel mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/ryu-devel
