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>
This commit is contained in:
Isaku Yamahata 2012-06-13 11:44:14 +09:00 committed by FUJITA Tomonori
parent 9319c3e8ce
commit bf51bb2f8c
2 changed files with 12 additions and 8 deletions

View File

@ -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))

View File

@ -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