blob: c0ecc07cdfc4f6286da1b760821766241ccf0ef8 [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)
94 rc = self.controller.message_send(m)
95 self.assertNotEqual(rc, -1, "Error sending set mirroring command")
96
97 def bsn_get_mirroring(self):
98 """
99 Use the BSN_GET_MIRRORING_REQUEST vendor command to get the
100 enabled/disabled state of mirror action support
101 """
102 m = message.vendor()
103 m.vendor = 0x005c16c7
104 m.data = struct.pack("!LBBBB", 4, 0, 0, 0, 0)
105 rc = self.controller.message_send(m)
106 self.assertNotEqual(rc, -1, "Error sending get mirroring command")
107 m, r = self.controller.poll(ofp.OFPT_VENDOR, 2)
108 self.assertEqual(m.vendor, 0x005c16c7, "Wrong vendor ID")
109 x = struct.unpack("!LBBBB", m.data)
110 self.assertEqual(x[0], 5, "Wrong subtype")
111 return x[1]
112
113 def runTest(self):
114 mirror_ports = test_param_get("mirror_ports")
115 ports = [p for p in config["port_map"].keys() if p not in mirror_ports]
116 pkt = simple_tcp_packet()
117 match = packet_to_flow_match(self, pkt)
118 match.in_port = ports[0]
119 match.wildcards &= ~ofp.OFPFW_IN_PORT
120
121 logging.info("Checking that mirror ports are not reported")
122 self.assertEqual(bool(self.bsn_get_mirroring()), False)
123 m, r = self.controller.transact(message.features_request(), 2)
124 p = dict([(pt.port_no, pt) for pt in m.ports])
125 self.assertFalse(mirror_ports[0] in p or mirror_ports[1] in p,
126 "Mirror port in features reply")
127
128 logging.info("Enabling mirror port reporting")
129 self.bsn_set_mirroring(True)
130
131 logging.info("Checking that mirror ports are reported")
132 self.assertEqual(bool(self.bsn_get_mirroring()), True)
133 m, r = self.controller.transact(message.features_request(), 2)
134 p = dict([(pt.port_no, pt) for pt in m.ports])
135 self.assertTrue(mirror_ports[0] in p and mirror_ports[1] in p,
136 "Mirror port not in features reply")
137 self.assertTrue(p[mirror_ports[0]].config & (1 << 31),
138 "Mirror port config flag not set in features reply")
139 self.assertTrue(p[mirror_ports[1]].config & (1 << 31),
140 "Mirror port config flag not set in features reply")
141
142 act1 = bsn_action_mirror()
143 act1.dest_port = mirror_ports[0]
144 act1.copy_stage = 0
145 act2 = bsn_action_mirror()
146 act2.dest_port = mirror_ports[1]
147 act2.copy_stage = 0
148 act3 = action.action_output()
149 act3.port = ports[1]
150 flow_mod = message.flow_mod()
151 flow_mod.match = match
152 self.assertTrue(flow_mod.actions.add(act1), "Could not add mirror action")
153 self.assertTrue(flow_mod.actions.add(act2), "Could not add mirror action")
154 self.assertTrue(flow_mod.actions.add(act3), "Could not add output action")
155 self.assertEqual(delete_all_flows(self.controller), 0,
156 "Failed to delete all flows")
157 self.assertNotEqual(self.controller.message_send(flow_mod), -1,
158 "Error installing flow mod")
159 self.assertEqual(do_barrier(self.controller), 0, "Barrier failed")
160
161 logging.info("Sending packet to port %s" % ports[0])
162 self.dataplane.send(ports[0], str(pkt))
163 logging.info("Checking that packet was received from output port %s, "
164 "mirror ports %s and %s" % (
165 ports[1], mirror_ports[0], mirror_ports[1]))
166 receive_pkt_check(self.dataplane, pkt,
167 [ports[1], mirror_ports[0], mirror_ports[1]], [],
168 self)