blob: ebf0f75dc396f387b89e71750d6ae30a0b0eeb54 [file] [log] [blame]
Ed Swierk47f608d2012-11-12 15:36:32 -08001"""
2"""
3import struct
4
5import logging
6
7from oftest import config
8import oftest.controller as controller
9import oftest.cstruct as ofp
10import oftest.message as message
11import oftest.action as action
12import oftest.action_list as action_list
13import oftest.base_tests as base_tests
14
15from oftest.testutils import *
16
17class bsn_action_mirror(action.action_vendor):
18 def __init__(self):
19 self.type = ofp.OFPAT_VENDOR
20 self.len = 24
21 self.vendor = 0x005c16c7
22 self.subtype = 1
23 self.dest_port = 0
24 self.vlan_tag = 0
25 self.copy_stage = 0
26
27 def __assert(self):
28 return (True, None)
29
30 def pack(self, assertstruct=True):
31 return struct.pack("!HHLLLLBBBB", self.type, self.len, self.vendor,
32 self.subtype, self.dest_port, self.vlan_tag,
33 self.copy_stage, 0, 0, 0)
34
35 def unpack(self, binaryString):
36 if len(binaryString) < self.len:
37 raise Exception("too short")
38 x = struct.unpack("!HHLLLLBBBB", binaryString[:self.len])
39 if x[0] != self.type:
40 raise Exception("wrong type")
41 if x[1] != self.len:
42 raise Exception("wrong length")
43 if x[2] != self.vendor:
44 raise Exception("wrong vendor")
45 if x[3] != self.subtype:
46 raise Exception("wrong subtype")
47 self.dest_port = x[4]
48 self.vlan_tag = x[5]
49 self.copy_stage = x[6]
50 return binaryString[self.len:]
51
52 def __len__(self):
53 return self.len
54
55 def __eq__(self, other):
56 if type(self) != type(other): return False
57 if self.type != other.type: return False
58 if self.len != other.len: return False
59 if self.vendor != other.vendor: return False
60 if self.subtype != other.subtype: return False
61 if self.dest_port != other.dest_port: return False
62 if self.vlan_tag != other.vlan_tag: return False
63 if self.copy_stage != other.copy_stage: return False
64 return True
65
66 def __ne__(self, other):
67 return not self.__eq__(other)
68
69 def show(self, prefix=""):
70 outstr = prefix + "action_vendor\n"
71 for f in ["type", "len", "vendor", "subtype", "dest_port", "vlan_tag",
72 "copy_stage"]:
73 outstr += prefix + ("%s: %s\n" % (f, getattr(self, f)))
74 return outstr
75
76action_list.action_object_map[ofp.OFPAT_VENDOR] = bsn_action_mirror
77
78class BSNMirrorAction(base_tests.SimpleDataPlane):
79 """
80 Exercise BSN vendor extension for copying packets to a mirror destination
81 port
82 """
83
84 priority = -1
85
86 def bsn_set_mirroring(self, enabled):
87 """
88 Use the BSN_SET_MIRRORING vendor command to enable/disable
89 mirror action support
90 """
91 m = message.vendor()
92 m.vendor = 0x005c16c7
93 m.data = struct.pack("!LBBBB", 3, enabled, 0, 0, 0)
Rich Lane5c3151c2013-01-03 17:15:41 -080094 self.controller.message_send(m)
Ed Swierk47f608d2012-11-12 15:36:32 -080095
96 def bsn_get_mirroring(self):
97 """
98 Use the BSN_GET_MIRRORING_REQUEST vendor command to get the
99 enabled/disabled state of mirror action support
100 """
101 m = message.vendor()
102 m.vendor = 0x005c16c7
103 m.data = struct.pack("!LBBBB", 4, 0, 0, 0, 0)
Rich Lane5c3151c2013-01-03 17:15:41 -0800104 self.controller.message_send(m)
Ed Swierk47f608d2012-11-12 15:36:32 -0800105 m, r = self.controller.poll(ofp.OFPT_VENDOR, 2)
106 self.assertEqual(m.vendor, 0x005c16c7, "Wrong vendor ID")
107 x = struct.unpack("!LBBBB", m.data)
108 self.assertEqual(x[0], 5, "Wrong subtype")
109 return x[1]
110
111 def runTest(self):
112 mirror_ports = test_param_get("mirror_ports")
113 ports = [p for p in config["port_map"].keys() if p not in mirror_ports]
114 pkt = simple_tcp_packet()
115 match = packet_to_flow_match(self, pkt)
116 match.in_port = ports[0]
117 match.wildcards &= ~ofp.OFPFW_IN_PORT
118
119 logging.info("Checking that mirror ports are not reported")
120 self.assertEqual(bool(self.bsn_get_mirroring()), False)
121 m, r = self.controller.transact(message.features_request(), 2)
122 p = dict([(pt.port_no, pt) for pt in m.ports])
123 self.assertFalse(mirror_ports[0] in p or mirror_ports[1] in p,
124 "Mirror port in features reply")
125
126 logging.info("Enabling mirror port reporting")
127 self.bsn_set_mirroring(True)
128
129 logging.info("Checking that mirror ports are reported")
130 self.assertEqual(bool(self.bsn_get_mirroring()), True)
131 m, r = self.controller.transact(message.features_request(), 2)
132 p = dict([(pt.port_no, pt) for pt in m.ports])
133 self.assertTrue(mirror_ports[0] in p and mirror_ports[1] in p,
134 "Mirror port not in features reply")
135 self.assertTrue(p[mirror_ports[0]].config & (1 << 31),
136 "Mirror port config flag not set in features reply")
137 self.assertTrue(p[mirror_ports[1]].config & (1 << 31),
138 "Mirror port config flag not set in features reply")
139
140 act1 = bsn_action_mirror()
141 act1.dest_port = mirror_ports[0]
142 act1.copy_stage = 0
143 act2 = bsn_action_mirror()
144 act2.dest_port = mirror_ports[1]
145 act2.copy_stage = 0
146 act3 = action.action_output()
147 act3.port = ports[1]
148 flow_mod = message.flow_mod()
149 flow_mod.match = match
Rich Lanee30455b2013-01-03 16:24:44 -0800150 flow_mod.actions.add(act1)
151 flow_mod.actions.add(act2)
152 flow_mod.actions.add(act3)
Rich Lane32bf9482013-01-03 17:26:30 -0800153 delete_all_flows(self.controller)
Rich Lane5c3151c2013-01-03 17:15:41 -0800154 self.controller.message_send(flow_mod)
Ed Swierk47f608d2012-11-12 15:36:32 -0800155 self.assertEqual(do_barrier(self.controller), 0, "Barrier failed")
156
157 logging.info("Sending packet to port %s" % ports[0])
158 self.dataplane.send(ports[0], str(pkt))
159 logging.info("Checking that packet was received from output port %s, "
160 "mirror ports %s and %s" % (
161 ports[1], mirror_ports[0], mirror_ports[1]))
162 receive_pkt_check(self.dataplane, pkt,
163 [ports[1], mirror_ports[0], mirror_ports[1]], [],
164 self)