blob: 2a9660cb39a945ca73af0e62610434d872fc5e26 [file] [log] [blame]
Rich Lane629393f2013-01-10 15:37:33 -08001import struct
2import match as match
3from match import oxm_tlv
4from binascii import b2a_hex
5from base_list import ofp_base_list
6
7
8class match_list(ofp_base_list):
9
10 def __init__(self):
11 ofp_base_list.__init__(self)
12 self.tlvs = self.items
13 self.name = "match"
14 self.class_list = match.match_class_list
15
16 def __len__(self):
17 return sum([len(i) for i in self])
18
19 def unpack(self, binary_string, bytes=None):
20 if bytes <= 4:
21 return binary_string[4:]
22 if bytes == None:
23 bytes = len(binary_string)
24 offset = 0
25 cur_string = binary_string
26 while offset < bytes:
27 read = 0
28 oxm_class, oxm_fieldhm, oxm_length = struct.unpack("!HBB", cur_string[read:read+4])
29 #Found padding bytes?
30 if not oxm_class:
31 break
32 oxm_field = oxm_fieldhm >> 1
33 oxm_hasmask = oxm_fieldhm & 0x00000001
34 payload = struct.unpack("!" + str(oxm_length) + "s", cur_string[read+4:read+4+oxm_length])[0]
35 if oxm_hasmask:
36 value, mask = payload[:oxm_length/2], payload[oxm_length/2:]
37 else:
38 value, mask = payload, None
39 oxm = oxm_tlv(oxm_field, oxm_hasmask, oxm_length, value,mask, oxm_class)
40 self.tlvs.append(oxm)
41 read = 4 + oxm_length
42 offset += read
43 cur_string = cur_string[read:]
44
45 return cur_string