blob: 3c79ffdc3f495f93be5b69dd54ce955cc0278750 [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
Rich Laned7b0ffa2013-03-08 15:53:42 -08009import ofp
Ed Swierk47f608d2012-11-12 15:36:32 -080010import oftest.base_tests as base_tests
11
12from oftest.testutils import *
13
Rich Lane9d3cc6b2013-03-08 16:33:08 -080014class bsn_action_mirror(ofp.action.vendor):
Ed Swierk47f608d2012-11-12 15:36:32 -080015 def __init__(self):
16 self.type = ofp.OFPAT_VENDOR
17 self.len = 24
18 self.vendor = 0x005c16c7
19 self.subtype = 1
20 self.dest_port = 0
21 self.vlan_tag = 0
22 self.copy_stage = 0
23
24 def __assert(self):
25 return (True, None)
26
27 def pack(self, assertstruct=True):
28 return struct.pack("!HHLLLLBBBB", self.type, self.len, self.vendor,
29 self.subtype, self.dest_port, self.vlan_tag,
30 self.copy_stage, 0, 0, 0)
31
32 def unpack(self, binaryString):
33 if len(binaryString) < self.len:
34 raise Exception("too short")
35 x = struct.unpack("!HHLLLLBBBB", binaryString[:self.len])
36 if x[0] != self.type:
37 raise Exception("wrong type")
38 if x[1] != self.len:
39 raise Exception("wrong length")
40 if x[2] != self.vendor:
41 raise Exception("wrong vendor")
42 if x[3] != self.subtype:
43 raise Exception("wrong subtype")
44 self.dest_port = x[4]
45 self.vlan_tag = x[5]
46 self.copy_stage = x[6]
47 return binaryString[self.len:]
48
49 def __len__(self):
50 return self.len
51
52 def __eq__(self, other):
53 if type(self) != type(other): return False
54 if self.type != other.type: return False
55 if self.len != other.len: return False
56 if self.vendor != other.vendor: return False
57 if self.subtype != other.subtype: return False
58 if self.dest_port != other.dest_port: return False
59 if self.vlan_tag != other.vlan_tag: return False
60 if self.copy_stage != other.copy_stage: return False
61 return True
62
63 def __ne__(self, other):
64 return not self.__eq__(other)
65
66 def show(self, prefix=""):
67 outstr = prefix + "action_vendor\n"
68 for f in ["type", "len", "vendor", "subtype", "dest_port", "vlan_tag",
69 "copy_stage"]:
70 outstr += prefix + ("%s: %s\n" % (f, getattr(self, f)))
71 return outstr
72
73action_list.action_object_map[ofp.OFPAT_VENDOR] = bsn_action_mirror
74
Rich Lane0a4f6372013-01-02 14:40:22 -080075@nonstandard
Ed Swierk47f608d2012-11-12 15:36:32 -080076class BSNMirrorAction(base_tests.SimpleDataPlane):
77 """
78 Exercise BSN vendor extension for copying packets to a mirror destination
79 port
80 """
81
Ed Swierk47f608d2012-11-12 15:36:32 -080082 def bsn_set_mirroring(self, enabled):
83 """
84 Use the BSN_SET_MIRRORING vendor command to enable/disable
85 mirror action support
86 """
Rich Lane28fa9272013-03-08 16:00:25 -080087 m = ofp.message.vendor()
Ed Swierk47f608d2012-11-12 15:36:32 -080088 m.vendor = 0x005c16c7
89 m.data = struct.pack("!LBBBB", 3, enabled, 0, 0, 0)
Rich Lane5c3151c2013-01-03 17:15:41 -080090 self.controller.message_send(m)
Ed Swierk47f608d2012-11-12 15:36:32 -080091
92 def bsn_get_mirroring(self):
93 """
94 Use the BSN_GET_MIRRORING_REQUEST vendor command to get the
95 enabled/disabled state of mirror action support
96 """
Rich Lane28fa9272013-03-08 16:00:25 -080097 m = ofp.message.vendor()
Ed Swierk47f608d2012-11-12 15:36:32 -080098 m.vendor = 0x005c16c7
99 m.data = struct.pack("!LBBBB", 4, 0, 0, 0, 0)
Rich Lane5c3151c2013-01-03 17:15:41 -0800100 self.controller.message_send(m)
Ed Swierk47f608d2012-11-12 15:36:32 -0800101 m, r = self.controller.poll(ofp.OFPT_VENDOR, 2)
102 self.assertEqual(m.vendor, 0x005c16c7, "Wrong vendor ID")
103 x = struct.unpack("!LBBBB", m.data)
104 self.assertEqual(x[0], 5, "Wrong subtype")
105 return x[1]
106
107 def runTest(self):
108 mirror_ports = test_param_get("mirror_ports")
109 ports = [p for p in config["port_map"].keys() if p not in mirror_ports]
110 pkt = simple_tcp_packet()
111 match = packet_to_flow_match(self, pkt)
112 match.in_port = ports[0]
113 match.wildcards &= ~ofp.OFPFW_IN_PORT
114
115 logging.info("Checking that mirror ports are not reported")
116 self.assertEqual(bool(self.bsn_get_mirroring()), False)
Rich Lane28fa9272013-03-08 16:00:25 -0800117 m, r = self.controller.transact(ofp.message.features_request(), 2)
Ed Swierk47f608d2012-11-12 15:36:32 -0800118 p = dict([(pt.port_no, pt) for pt in m.ports])
119 self.assertFalse(mirror_ports[0] in p or mirror_ports[1] in p,
120 "Mirror port in features reply")
121
122 logging.info("Enabling mirror port reporting")
123 self.bsn_set_mirroring(True)
124
125 logging.info("Checking that mirror ports are reported")
126 self.assertEqual(bool(self.bsn_get_mirroring()), True)
Rich Lane28fa9272013-03-08 16:00:25 -0800127 m, r = self.controller.transact(ofp.message.features_request(), 2)
Ed Swierk47f608d2012-11-12 15:36:32 -0800128 p = dict([(pt.port_no, pt) for pt in m.ports])
129 self.assertTrue(mirror_ports[0] in p and mirror_ports[1] in p,
130 "Mirror port not in features reply")
131 self.assertTrue(p[mirror_ports[0]].config & (1 << 31),
132 "Mirror port config flag not set in features reply")
133 self.assertTrue(p[mirror_ports[1]].config & (1 << 31),
134 "Mirror port config flag not set in features reply")
135
136 act1 = bsn_action_mirror()
137 act1.dest_port = mirror_ports[0]
138 act1.copy_stage = 0
139 act2 = bsn_action_mirror()
140 act2.dest_port = mirror_ports[1]
141 act2.copy_stage = 0
Rich Lane9d3cc6b2013-03-08 16:33:08 -0800142 act3 = ofp.action.output()
Ed Swierk47f608d2012-11-12 15:36:32 -0800143 act3.port = ports[1]
Rich Lane28fa9272013-03-08 16:00:25 -0800144 flow_mod = ofp.message.flow_mod()
Ed Swierk47f608d2012-11-12 15:36:32 -0800145 flow_mod.match = match
Rich Lanee30455b2013-01-03 16:24:44 -0800146 flow_mod.actions.add(act1)
147 flow_mod.actions.add(act2)
148 flow_mod.actions.add(act3)
Rich Lane32bf9482013-01-03 17:26:30 -0800149 delete_all_flows(self.controller)
Rich Lane5c3151c2013-01-03 17:15:41 -0800150 self.controller.message_send(flow_mod)
Rich Lane3a261d52013-01-03 17:45:08 -0800151 do_barrier(self.controller)
Ed Swierk47f608d2012-11-12 15:36:32 -0800152
153 logging.info("Sending packet to port %s" % ports[0])
154 self.dataplane.send(ports[0], str(pkt))
155 logging.info("Checking that packet was received from output port %s, "
156 "mirror ports %s and %s" % (
157 ports[1], mirror_ports[0], mirror_ports[1]))
158 receive_pkt_check(self.dataplane, pkt,
159 [ports[1], mirror_ports[0], mirror_ports[1]], [],
160 self)