stplib: Fix to compare MAC address and Bridge ID

cmp() func was introduced for Python 3 compatibility before,
but this implementation is not enough, because a MAC address
can not be compared with a Bridge ID (integer value) by com() func.

This patch fixes to convert the MAC address into an integer value
before comparing with Bridge ID and fixes this problem.

Signed-off-by: Shinpei Muraoka <shinpei.muraoka@gmail.com>
Signed-off-by: FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp>
This commit is contained in:
Shinpei Muraoka 2016-09-27 12:01:04 +09:00 committed by FUJITA Tomonori
parent 29d1a97139
commit d23c07054e
2 changed files with 13 additions and 2 deletions

View File

@ -49,6 +49,15 @@ def haddr_to_str(addr):
raise AssertionError raise AssertionError
def haddr_to_int(addr):
"""Convert mac address string in human readable format into
integer value"""
try:
return int(addr.replace(':', ''), 16)
except:
raise ValueError
def haddr_to_bin(string): def haddr_to_bin(string):
"""Parse mac address string in human readable format into """Parse mac address string in human readable format into
internal representation""" internal representation"""

View File

@ -25,6 +25,7 @@ from ryu.controller.handler import set_ev_cls
from ryu.exception import RyuException from ryu.exception import RyuException
from ryu.exception import OFPUnknownVersion from ryu.exception import OFPUnknownVersion
from ryu.lib import hub from ryu.lib import hub
from ryu.lib import mac
from ryu.lib.dpid import dpid_to_str from ryu.lib.dpid import dpid_to_str
from ryu.lib.packet import bpdu from ryu.lib.packet import bpdu
from ryu.lib.packet import ethernet from ryu.lib.packet import ethernet
@ -351,7 +352,8 @@ class Stp(app_manager.RyuApp):
if not result: if not result:
result1 = Stp._cmp_value( result1 = Stp._cmp_value(
rcv_priority.designated_bridge_id.value, rcv_priority.designated_bridge_id.value,
my_priority.designated_bridge_id.mac_addr) mac.haddr_to_int(
my_priority.designated_bridge_id.mac_addr))
result2 = Stp._cmp_value( result2 = Stp._cmp_value(
rcv_priority.designated_port_id.value, rcv_priority.designated_port_id.value,
my_priority.designated_port_id.port_no) my_priority.designated_port_id.port_no)
@ -363,7 +365,7 @@ class Stp(app_manager.RyuApp):
@staticmethod @staticmethod
def _cmp_value(value1, value2): def _cmp_value(value1, value2):
result = cmp(str(value1), str(value2)) result = cmp(value1, value2)
if result < 0: if result < 0:
return SUPERIOR return SUPERIOR
elif result == 0: elif result == 0: