summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorIWAMOTO Toshihiro <iwamoto@valinux.co.jp>2017-05-09 11:00:27 +0900
committerFUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp>2017-05-11 12:32:18 +0900
commit607f4d827674ab294a4155a65c987de559883296 (patch)
tree73a5b56b0c2895cf9fc5ddd0a61c931066a34cb6
parenta197d87375808c4f99b48af3852eb5d1c986a6a9 (diff)
ofproto: Allow CIDR notation for ipv[46]_{src, dst}
This hack allows CIDR notations for OFPMatch. For example: ofpp.OFPMatch(ipv4_src="198.51.100.0/24") Signed-off-by: IWAMOTO Toshihiro <iwamoto@valinux.co.jp> Signed-off-by: FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp>
-rw-r--r--ryu/lib/addrconv.py20
-rw-r--r--ryu/ofproto/oxx_fields.py3
2 files changed, 19 insertions, 4 deletions
diff --git a/ryu/lib/addrconv.py b/ryu/lib/addrconv.py
index 76417501..36a00fa2 100644
--- a/ryu/lib/addrconv.py
+++ b/ryu/lib/addrconv.py
@@ -18,20 +18,32 @@ import netaddr
class AddressConverter(object):
- def __init__(self, addr, strat, **kwargs):
+ def __init__(self, addr, strat, fallback=None, **kwargs):
self._addr = addr
self._strat = strat
+ self._fallback = fallback
self._addr_kwargs = kwargs
def text_to_bin(self, text):
- return self._addr(text, **self._addr_kwargs).packed
+ try:
+ return self._addr(text, **self._addr_kwargs).packed
+ except Exception as e:
+ if self._fallback is None:
+ raise e
+
+ # text_to_bin is expected to return binary string under
+ # normal circumstances. See ofproto.oxx_fields._from_user.
+ ip = self._fallback(text, **self._addr_kwargs)
+ return ip.ip.packed, ip.netmask.packed
def bin_to_text(self, bin):
return str(self._addr(self._strat.packed_to_int(bin),
**self._addr_kwargs))
-ipv4 = AddressConverter(netaddr.IPAddress, netaddr.strategy.ipv4, version=4)
-ipv6 = AddressConverter(netaddr.IPAddress, netaddr.strategy.ipv6, version=6)
+ipv4 = AddressConverter(netaddr.IPAddress, netaddr.strategy.ipv4,
+ fallback=netaddr.IPNetwork, version=4)
+ipv6 = AddressConverter(netaddr.IPAddress, netaddr.strategy.ipv6,
+ fallback=netaddr.IPNetwork, version=6)
class mac_mydialect(netaddr.mac_unix):
diff --git a/ryu/ofproto/oxx_fields.py b/ryu/ofproto/oxx_fields.py
index e9c1fb97..e8477de6 100644
--- a/ryu/ofproto/oxx_fields.py
+++ b/ryu/ofproto/oxx_fields.py
@@ -72,6 +72,9 @@ def _from_user(oxx, name_to_field, name, user_value):
value = t.from_user(value)
if mask is not None:
mask = t.from_user(mask)
+ elif isinstance(value, tuple):
+ # This hack is to accomodate CIDR notations with IPv[46]Addr.
+ value, mask = value
return num, value, mask