Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added a way to handle 4096/4096 and 0 #98

Merged
merged 12 commits into from
Mar 9, 2023
14 changes: 14 additions & 0 deletions tests/integration/v0x04/test_match.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,17 @@ def test_dl_vlan_pyof(self):
of_tlv = expected.as_of_tlv()
actual = MatchDLVLAN.from_of_tlv(of_tlv)
self.assertEqual(expected, actual)

def test_dl_vlan_mask_pyof(self):
Alopalao marked this conversation as resolved.
Show resolved Hide resolved
"""Convert to and from pyog OxmTLV with mask"""
expected = MatchDLVLAN("4096/4096")
of_tlv = expected.as_of_tlv()
actual = MatchDLVLAN.from_of_tlv(of_tlv)
self.assertEqual(expected, actual)

def test_dl_vlan_zero_pyof(self):
"""Convert to and from pyog OxmTLV with 0"""
expected = MatchDLVLAN(0)
of_tlv = expected.as_of_tlv()
actual = MatchDLVLAN.from_of_tlv(of_tlv)
self.assertEqual(expected, actual)
20 changes: 16 additions & 4 deletions v0x04/match_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,10 @@ def as_of_tlv(self):
except ValueError:
value, mask = map(int, self.value.split('/'))
oxm_hasmask = True
value = value | VlanId.OFPVID_PRESENT
if value == 0:
value = VlanId.OFPVID_NONE
else:
value = value | VlanId.OFPVID_PRESENT
value_bytes = value.to_bytes(2, 'big')
if mask:
Alopalao marked this conversation as resolved.
Show resolved Hide resolved
mask = mask | VlanId.OFPVID_PRESENT
Expand All @@ -53,11 +56,20 @@ def as_of_tlv(self):

@classmethod
def from_of_tlv(cls, tlv):
"""Return an instance from a pyof OXM TLV."""
vlan_id = int.from_bytes(tlv.oxm_value[:2], 'big') & 4095
"""Return an instance from a pyof OXM TLV.
*oxm_value=0x1000/0x1000 is a valid case. It's necessary to
deserialize 0x1000 as 4096 thus it is ignore to not obtain 0
"""
vlan_id = int.from_bytes(tlv.oxm_value[:2], 'big')
viniarck marked this conversation as resolved.
Show resolved Hide resolved
if vlan_id != 4096:
# Ignore 4096
vlan_id &= 4095
value = vlan_id
if tlv.oxm_hasmask:
vlan_mask = int.from_bytes(tlv.oxm_value[2:], 'big') & 4095
vlan_mask = int.from_bytes(tlv.oxm_value[2:], 'big')
if vlan_mask != 4096:
# Ignore 4096
vlan_mask &= 4095
value = f'{vlan_id}/{vlan_mask}'
return cls(value)

Expand Down