On Wed, 24 Dec 2014 18:38:30 +0800 Wei-Li Tang <[email protected]> wrote:
> Signed-off-by: Wei-Li Tang <[email protected]> > --- > ryu/lib/ofctl_v1_2.py | 27 +++++++++++++++++++++++---- > ryu/lib/ofctl_v1_3.py | 27 +++++++++++++++++++++++---- > 2 files changed, 46 insertions(+), 8 deletions(-) > > diff --git a/ryu/lib/ofctl_v1_2.py b/ryu/lib/ofctl_v1_2.py > index c8b9174..5e056d1 100644 > --- a/ryu/lib/ofctl_v1_2.py > +++ b/ryu/lib/ofctl_v1_2.py > @@ -17,7 +17,6 @@ import base64 > import struct > import socket > import logging > -import netaddr > > from ryu.ofproto import ether > from ryu.ofproto import inet > @@ -284,10 +283,30 @@ def to_match_eth(value): > > > def to_match_ip(value): > + # NOTE: We don't use netaddr.ip.IPNetwork() to resolve address here > + # because it doesn't support arbitrary bitmask. > if '/' in value: > - ip = netaddr.ip.IPNetwork(value) > - ip_addr = str(ip.ip) > - ip_mask = str(ip.netmask) > + (ip_addr, ip_mask) = value.split('/') > + > + # Convert ip_mask into dotted-decimal or colon-hexadecimal format > + # if value is represented in CIDR notation. > + if ip_mask.isdigit(): > + # Identify the family of the address. > + if ':' in ip_addr: > + # IPv6 CIDR > + UINT128_MAX = (1 << 128) - 1 > + ip_mask_int = UINT128_MAX & UINT128_MAX << 128 - int(ip_mask) > + ip_mask_h = ip_mask_int >> 64 > + ip_mask_l = ip_mask_int & ofproto_v1_2_parser.UINT64_MAX > + ip_mask = socket.inet_ntop(socket.AF_INET6, > + struct.pack('!2Q', > + ip_mask_h, ip_mask_l)) > + else: > + # IPv4 CIDR > + ip_mask_int = ofproto_v1_2_parser.UINT32_MAX & \ > + ofproto_v1_2_parser.UINT32_MAX << 32 - int(ip_mask) > + ip_mask = socket.inet_ntoa(struct.pack('!I', ip_mask_int)) How about something like the following? We don't need to handle an arbitrary bitmask such as "192.168.0.1/255.255.0.255" here except for splitting, right? What we need to handle here is non dotted-decimal representation (like 1.1.1.1/24), which can't be an arbitrary bitmask? diff --git a/ryu/lib/ofctl_v1_3.py b/ryu/lib/ofctl_v1_3.py index 485a3b4..4372e53 100644 --- a/ryu/lib/ofctl_v1_3.py +++ b/ryu/lib/ofctl_v1_3.py @@ -306,9 +306,11 @@ def to_match_eth(value): def to_match_ip(value): if '/' in value: - ip = netaddr.ip.IPNetwork(value) - ip_addr = str(ip.ip) - ip_mask = str(ip.netmask) + (ip_addr, ip_mask) = value.split('/') + if ip_mask.isdigit(): + ip = netaddr.ip.IPNetwork(value) + ip_addr = str(ip.ip) + ip_mask = str(ip.netmask) return ip_addr, ip_mask else: return value ------------------------------------------------------------------------------ Dive into the World of Parallel Programming! The Go Parallel Website, sponsored by Intel and developed in partnership with Slashdot Media, is your hub for all things parallel software development, from weekly thought leadership blogs to news, videos, case studies, tutorials and more. Take a look and join the conversation now. http://goparallel.sourceforge.net _______________________________________________ Ryu-devel mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/ryu-devel
