summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorIsaku Yamahata <yamahata@valinux.co.jp>2012-06-13 11:44:14 +0900
committerFUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp>2012-06-16 09:02:43 +0900
commitbf51bb2f8c165757aea363fc84c7e2cd7747127c (patch)
tree354497147a1a23e9cb00b2aafea9aa658e4fad5c
parent9319c3e8ce26b79538a8199b2c53aaff83fac560 (diff)
ofproto/nx_match: simplify set_dl_{src, dst}_mask()
Introduce helper function for mac address, and simplify set_dl_{src, dst}_mask() Signed-off-by: Isaku Yamahata <yamahata@valinux.co.jp> Signed-off-by: FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp>
-rw-r--r--ryu/lib/mac.py12
-rw-r--r--ryu/ofproto/nx_match.py8
2 files changed, 12 insertions, 8 deletions
diff --git a/ryu/lib/mac.py b/ryu/lib/mac.py
index 14218d34..20826435 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 81922518..7a8bca7b 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