blob: 10c3f93e6521f04e449065534026e36dbce2c082 [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
Rich Lane0a4f6372013-01-02 14:40:22 -080078@nonstandard
Ed Swierk47f608d2012-11-12 15:36:32 -080079class BSNMirrorAction(base_tests.SimpleDataPlane):
80 """
81 Exercise BSN vendor extension for copying packets to a mirror destination
82 port
83 """
84
Ed Swierk47f608d2012-11-12 15:36:32 -080085 def bsn_set_mirroring(self, enabled):
86 """
87 Use the BSN_SET_MIRRORING vendor command to enable/disable
88 mirror action support
89 """
90 m = message.vendor()
91 m.vendor = 0x005c16c7
92 m.data = struct.pack("!LBBBB", 3, enabled, 0, 0, 0)
Rich Lane5c3151c2013-01-03 17:15:41 -080093 self.controller.message_send(m)
Ed Swierk47f608d2012-11-12 15:36:32 -080094
95 def bsn_get_mirroring(self):
96 """
97 Use the BSN_GET_MIRRORING_REQUEST vendor command to get the
98 enabled/disabled state of mirror action support
99 """
100 m = message.vendor()
101 m.vendor = 0x005c16c7
102 m.data = struct.pack("!LBBBB", 4, 0, 0, 0, 0)
Rich Lane5c3151c2013-01-03 17:15:41 -0800103 self.controller.message_send(m)
Ed Swierk47f608d2012-11-12 15:36:32 -0800104 m, r = self.controller.poll(ofp.OFPT_VENDOR, 2)
105 self.assertEqual(m.vendor, 0x005c16c7, "Wrong vendor ID")
106 x = struct.unpack("!LBBBB", m.data)
107 self.assertEqual(x[0], 5, "Wrong subtype")
108 return x[1]
109
110 def runTest(self):
111 mirror_ports = test_param_get("mirror_ports")
112 ports = [p for p in config["port_map"].keys() if p not in mirror_ports]
113 pkt = simple_tcp_packet()
114 match = packet_to_flow_match(self, pkt)
115 match.in_port = ports[0]
116 match.wildcards &= ~ofp.OFPFW_IN_PORT
117
118 logging.info("Checking that mirror ports are not reported")
119 self.assertEqual(bool(self.bsn_get_mirroring()), False)
120 m, r = self.controller.transact(message.features_request(), 2)
121 p = dict([(pt.port_no, pt) for pt in m.ports])
122 self.assertFalse(mirror_ports[0] in p or mirror_ports[1] in p,
123 "Mirror port in features reply")
124
125 logging.info("Enabling mirror port reporting")
126 self.bsn_set_mirroring(True)
127
128 logging.info("Checking that mirror ports are reported")
129 self.assertEqual(bool(self.bsn_get_mirroring()), True)
130 m, r = self.controller.transact(message.features_request(), 2)
131 p = dict([(pt.port_no, pt) for pt in m.ports])
132 self.assertTrue(mirror_ports[0] in p and mirror_ports[1] in p,
133 "Mirror port not in features reply")
134 self.assertTrue(p[mirror_ports[0]].config & (1 << 31),
135 "Mirror port config flag not set in features reply")
136 self.assertTrue(p[mirror_ports[1]].config & (1 << 31),
137 "Mirror port config flag not set in features reply")
138
139 act1 = bsn_action_mirror()
140 act1.dest_port = mirror_ports[0]
141 act1.copy_stage = 0
142 act2 = bsn_action_mirror()
143 act2.dest_port = mirror_ports[1]
144 act2.copy_stage = 0
145 act3 = action.action_output()
146 act3.port = ports[1]
147 flow_mod = message.flow_mod()
148 flow_mod.match = match
Rich Lanee30455b2013-01-03 16:24:44 -0800149 flow_mod.actions.add(act1)
150 flow_mod.actions.add(act2)
151 flow_mod.actions.add(act3)
Rich Lane32bf9482013-01-03 17:26:30 -0800152 delete_all_flows(self.controller)
Rich Lane5c3151c2013-01-03 17:15:41 -0800153 self.controller.message_send(flow_mod)
Rich Lane3a261d52013-01-03 17:45:08 -0800154 do_barrier(self.controller)
Ed Swierk47f608d2012-11-12 15:36:32 -0800155
156 logging.info("Sending packet to port %s" % ports[0])
157 self.dataplane.send(ports[0], str(pkt))
158 logging.info("Checking that packet was received from output port %s, "
159 "mirror ports %s and %s" % (
160 ports[1], mirror_ports[0], mirror_ports[1]))
161 receive_pkt_check(self.dataplane, pkt,
162 [ports[1], mirror_ports[0], mirror_ports[1]], [],
163 self)