• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1% Tests for OpenFlow v1.3 with Scapy
2
3+ Preparation
4= Be sure we have loaded OpenFlow v3
5load_contrib("openflow3")
6
7+ Usual OFv1.3 messages
8
9= OFPTHello(), hello without version bitmap
10ofm = OFPTHello()
11raw(ofm) == b'\x04\x00\x00\x08\x00\x00\x00\x00'
12
13= OFPTEchoRequest(), echo request
14ofm = OFPTEchoRequest()
15raw(ofm) == b'\x04\x02\x00\x08\x00\x00\x00\x00'
16
17= OFPMatch(), check padding
18ofm = OFPMatch(oxm_fields=OFBEthType(eth_type=0x86dd))
19assert len(raw(ofm))%8 == 0
20raw(ofm) == b'\x00\x01\x00\x0a\x80\x00\x0a\x02\x86\xdd\x00\x00\x00\x00\x00\x00'
21
22= OpenFlow3(), generic method test with OFPTEchoRequest()
23ofm = OFPTEchoRequest()
24s = raw(ofm)
25isinstance(OpenFlow3(s), OFPTEchoRequest)
26
27= OFPTFlowMod(), check codes and defaults values
28ofm = OFPTFlowMod(cmd='OFPFC_DELETE', out_group='ALL', flags='CHECK_OVERLAP+NO_PKT_COUNTS')
29assert ofm.cmd == 3
30assert ofm.out_port == 0xffffffff
31assert ofm.out_group == 0xfffffffc
32ofm.flags == 10
33
34= OFBIPv6ExtHdrHMID(), check creation of last OXM classes
35assert hasattr(OFBIPv6ExtHdr(), 'ipv6_ext_hdr_flags')
36OFBIPv6ExtHdrHMID().field == 39
37
38+ Complex OFv1.3 messages
39
40= OFPTFlowMod(), complex flow_mod
41mtc = OFPMatch(oxm_fields=OFBVLANVID(vlan_vid=10))
42ist1 = OFPITApplyActions(actions=[OFPATSetField(field=OFBIPv4Src(ipv4_src='192.168.10.41')),OFPATSetField(field=OFBEthSrc(eth_src='1a:d5:cb:4e:3c:64')),OFPATOutput(port='NORMAL')])
43ist2 = OFPITWriteActions(actions=OFPATOutput(port='CONTROLLER'))
44ofm = OFPTFlowMod(table_id=2, match=mtc, instructions=[ist1,ist2])
45hexdump(ofm)
46s = b'\x04\x0e\x00\x98\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x00\x00\x00\x00\x00\x00\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\x00\x00\x00\x00\x00\x01\x00\n\x80\x00\x0c\x02\x00\n\x00\x00\x00\x00\x00\x00\x00\x04\x00@\x00\x00\x00\x00\x00\x19\x00\x18\x80\x00\n\x02\x08\x00\x80\x00\x16\x04\xc0\xa8\n)\x00\x00\x00\x00\x00\x00\x00\x19\x00\x10\x80\x00\x08\x06\x1a\xd5\xcbN<d\x00\x00\x00\x00\x00\x10\xff\xff\xff\xfa\xff\xff\x00\x00\x00\x00\x00\x00\x00\x03\x00\x18\x00\x00\x00\x00\x00\x00\x00\x10\xff\xff\xff\xfd\xff\xff\x00\x00\x00\x00\x00\x00'
47raw(ofm) == s
48
49= OFPTFlowMod(), complex flow_mod - Dissection
50ofm = OFPTFlowMod(raw(ofm))
51assert len(ofm.instructions[0].actions) == 3
52assert len(ofm.instructions[1].actions) == 1
53assert isinstance(ofm.instructions[0].actions[0].field[0], OFBEthType)
54assert ofm.instructions[0].actions[0].field[1].ipv4_src == '192.168.10.41'
55
56= OFPFlowStats()
57fls = OFPFlowStats(instructions=OFPITGotoTable(table_id=0))
58fls = OFPFlowStats(raw(fls))
59assert fls.match.type == 1
60assert fls.instructions[0].type == 1
61assert fls.instructions[0].table_id == 0
62
63= OFPETBadRequest() containing a flow_mod with wrong table_id
64flowmod = OFPTFlowMod(instructions=OFPITGotoTable(table_id=0))
65ofm = OFPETBadRequest(errcode='OFPBRC_BAD_TABLE_ID', data=raw(flowmod))
66hexdump(ofm)
67s = b'\x04\x01\x00L\x00\x00\x00\x00\x00\x01\x00\t\x04\x0e\x00@\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\x00\x00\x00\x00\x00\x01\x00\x04\x00\x00\x00\x00\x00\x01\x00\x08\x00\x00\x00\x00'
68raw(ofm) == s
69
70= OFPTFPTInstructions()
71fpti = OFPTFPTInstructions(instruction_ids=[OFPITGotoTableID(), OFPITClearActionsID()])
72fpti = OFPTFPTInstructions(raw(fpti))
73assert len(fpti) == 16
74assert fpti.instruction_ids[0].type == 1
75assert fpti.instruction_ids[1].type == 5
76
77fpti.instruction_ids[0] = OFPITGotoTableID()
78assert bytes(fpti) == b'\x00\x00\x00\x0c\x00\x01\x00\x04\x00\x05\x00\x04\x00\x00\x00\x00'
79
80= OFPTPacketIn() containing an Ethernet frame
81ofm = OFPTPacketIn(data=Ether()/IP()/ICMP())
82p = OFPTPacketIn(raw(ofm))
83dat = p.data
84assert isinstance(dat, Ether)
85assert isinstance(dat.payload, IP)
86isinstance(dat.payload.payload, ICMP)
87
88= OFPTGroupMod()
89fptgm = OFPTGroupMod(buckets=[OFPBucket(actions=[OFPATOutput(port='CONTROLLER')])])
90fptgm = OFPTGroupMod(raw(fptgm))
91assert fptgm.buckets[0].actions[0].port == 4294967293
92assert fptgm.buckets[0].actions[0].len == len(fptgm.buckets[0].actions[0]) == 16
93assert fptgm.buckets[0].actions[0].type == 0
94assert fptgm.buckets[0].actions[0].max_len == 0xffff
95
96= OFPTQueueGetConfigReply()
97qgcr = OFPTQueueGetConfigReply(queues=[OFPPacketQueue(queue_id=0, properties=[OFPQTNone()]), OFPPacketQueue(queue_id=1, properties=[OFPQTNone(), OFPQTMinRate(rate=123)])])
98qgcr = OFPTQueueGetConfigReply(raw(qgcr))
99assert qgcr.queues[0].queue_id == 0
100assert len(qgcr.queues[0].properties) == 1
101assert qgcr.queues[0].properties[0].type == 0
102
103assert qgcr.queues[1].queue_id == 1
104assert len(qgcr.queues[1].properties) == 2
105assert qgcr.queues[1].properties[0].type == 0
106assert qgcr.queues[1].properties[1].type == 1
107assert qgcr.queues[1].properties[1].rate == 123
108
109= OFPMPReplyMeter()
110rm = OFPMPReplyMeter(flags="REPLY_MORE", meter_stats=[OFPMeterStats(meter_id=2, duration_sec=3, byte_in_count=15, band_stats=[OFPMeterBandStats(packet_band_count=0x123, byte_band_count=0x456)])])
111rm = OFPMPReplyMeter(raw(rm))
112assert rm.flags == 1
113assert rm.meter_stats[0].meter_id == 2
114assert rm.meter_stats[0].duration_sec == 3
115assert rm.meter_stats[0].byte_in_count == 15
116assert rm.meter_stats[0].band_stats[0].packet_band_count == 0x123
117assert rm.meter_stats[0].band_stats[0].byte_band_count == 0x456
118
119= OFPMPReplyMeterConfig()
120rmc = OFPMPReplyMeterConfig(meter_configs=[OFPMeterConfig(flags="KBPS+STATS", bands=[OFPMBTDrop()]), OFPMeterConfig(bands=[OFPMBTDSCPRemark(burst_size=12)])])
121rmc = OFPMPReplyMeterConfig(raw(rmc))
122assert rmc.meter_configs[0].flags == 9
123assert rmc.meter_configs[0].bands[0].type == 0
124assert rmc.meter_configs[1].bands[0].type == 1
125assert rmc.meter_configs[1].bands[0].burst_size == 12
126
127+ Layer bindings
128
129= TCP()/OFPMPRequestDesc(), check default sport
130p = TCP()/OFPMPRequestDesc()
131p[TCP].sport == 6653
132
133= TCP()/OFPETHelloFailed(), check default dport
134p = TCP()/OFPETHelloFailed()
135p[TCP].dport == 6653
136
137= TCP()/OFPTHello() dissection, check new TCP.guess_payload_class
138o = TCP()/OFPTHello()
139p = TCP(raw(o))
140p[TCP].sport == 6653
141isinstance(p[TCP].payload, OFPTHello)
142
143= Advanced TCP()/OFPTHello() built, with OFPHETVersionBitmap
144pkt = TCP()/OFPTHello(elements=[OFPHETVersionBitmap(bitmap="OFv1.4", len=None)])
145pkt = TCP(raw(pkt))
146assert isinstance(pkt[OFPTHello].elements[0], OFPHETVersionBitmap)
147assert pkt[OFPTHello].elements[0].bitmap == 32
148assert pkt[OFPTHello].elements[0].len == 8
149
150pkt = TCP()/OFPTHello(elements=[OFPHETVersionBitmap(bitmap="OFv1.5", len=None)])
151pkt = TCP(raw(pkt))
152assert isinstance(pkt[OFPTHello].elements[0], OFPHETVersionBitmap)
153assert pkt[OFPTHello].elements[0].bitmap == 64
154assert pkt[OFPTHello].elements[0].len == 8
155
156= complete Ether()/IP()/TCP()/OFPTFeaturesRequest()
157ofm = Ether(src='00:11:22:33:44:55',dst='01:23:45:67:89:ab')/IP(src='10.0.0.7',dst='192.168.0.42')/TCP(sport=6633)/OFPTFeaturesRequest(xid=23)
158s = b'\x01#Eg\x89\xab\x00\x11"3DU\x08\x00E\x00\x000\x00\x01\x00\x00@\x06\xaf\xee\n\x00\x00\x07\xc0\xa8\x00*\x19\xe9\x19\xfd\x00\x00\x00\x00\x00\x00\x00\x00P\x02 \x00\x8c\xf7\x00\x00\x04\x05\x00\x08\x00\x00\x00\x17'
159assert raw(ofm) == s
160e = Ether(s)
161e.show2()
162e[OFPTFeaturesRequest].xid == 23
163
164= OFPMPRequestTableFeatures() with OFPTFPTMatch() OXMIDPacketListField tests
165pkt = TCP()/OFPMPRequestTableFeatures(table_features=[OFPTableFeatures(properties=[OFPTFPTMatch(oxm_ids=[OFBUDPSrcID()])])])
166assert raw(pkt) == b'\x19\xfd\x19\xfd\x00\x00\x00\x00\x00\x00\x00\x00P\x02 \x00\x00\x00\x00\x00\x04\x12\x00X\x00\x00\x00\x00\x00\x0c\x00\x00\x00\x00\x00\x00\x00H\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x08\x00\x08\x80\x00\x1e\x02'
167pkt = TCP(raw(pkt))
168assert pkt.table_features[0].properties[0].oxm_ids[0].fields == {'class_': 32768, 'field': 15, 'hasmask': 0, 'len': 2}
169
170= Test OFBTCPSrc Autocompletion
171
172conf.contribs['OPENFLOW']['prereq_autocomplete'] = True
173
174pkt = TCP()/OFPTPacketIn(match=OFPMatch(oxm_fields=OFBTCPSrc()))
175pkt = TCP(raw(pkt))
176
177assert len(pkt[OFPTPacketIn].match.oxm_fields) == 3
178assert isinstance(pkt[OFPTPacketIn].match.oxm_fields[0], OFBEthType)
179assert isinstance(pkt[OFPTPacketIn].match.oxm_fields[1], OFBIPProto)
180assert isinstance(pkt[OFPTPacketIn].match.oxm_fields[2], OFBTCPSrc)
181