Ed Swierk | 47f608d | 2012-11-12 15:36:32 -0800 | [diff] [blame] | 1 | """ |
| 2 | """ |
| 3 | import struct |
| 4 | |
| 5 | import logging |
| 6 | |
| 7 | from oftest import config |
| 8 | import oftest.controller as controller |
Rich Lane | d7b0ffa | 2013-03-08 15:53:42 -0800 | [diff] [blame] | 9 | import ofp |
Ed Swierk | 47f608d | 2012-11-12 15:36:32 -0800 | [diff] [blame] | 10 | import oftest.base_tests as base_tests |
| 11 | |
| 12 | from oftest.testutils import * |
| 13 | |
Rich Lane | 28fa927 | 2013-03-08 16:00:25 -0800 | [diff] [blame^] | 14 | class bsn_action_mirror(ofp.action.action_vendor): |
Ed Swierk | 47f608d | 2012-11-12 15:36:32 -0800 | [diff] [blame] | 15 | 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 | |
| 73 | action_list.action_object_map[ofp.OFPAT_VENDOR] = bsn_action_mirror |
| 74 | |
Rich Lane | 0a4f637 | 2013-01-02 14:40:22 -0800 | [diff] [blame] | 75 | @nonstandard |
Ed Swierk | 47f608d | 2012-11-12 15:36:32 -0800 | [diff] [blame] | 76 | class BSNMirrorAction(base_tests.SimpleDataPlane): |
| 77 | """ |
| 78 | Exercise BSN vendor extension for copying packets to a mirror destination |
| 79 | port |
| 80 | """ |
| 81 | |
Ed Swierk | 47f608d | 2012-11-12 15:36:32 -0800 | [diff] [blame] | 82 | def bsn_set_mirroring(self, enabled): |
| 83 | """ |
| 84 | Use the BSN_SET_MIRRORING vendor command to enable/disable |
| 85 | mirror action support |
| 86 | """ |
Rich Lane | 28fa927 | 2013-03-08 16:00:25 -0800 | [diff] [blame^] | 87 | m = ofp.message.vendor() |
Ed Swierk | 47f608d | 2012-11-12 15:36:32 -0800 | [diff] [blame] | 88 | m.vendor = 0x005c16c7 |
| 89 | m.data = struct.pack("!LBBBB", 3, enabled, 0, 0, 0) |
Rich Lane | 5c3151c | 2013-01-03 17:15:41 -0800 | [diff] [blame] | 90 | self.controller.message_send(m) |
Ed Swierk | 47f608d | 2012-11-12 15:36:32 -0800 | [diff] [blame] | 91 | |
| 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 Lane | 28fa927 | 2013-03-08 16:00:25 -0800 | [diff] [blame^] | 97 | m = ofp.message.vendor() |
Ed Swierk | 47f608d | 2012-11-12 15:36:32 -0800 | [diff] [blame] | 98 | m.vendor = 0x005c16c7 |
| 99 | m.data = struct.pack("!LBBBB", 4, 0, 0, 0, 0) |
Rich Lane | 5c3151c | 2013-01-03 17:15:41 -0800 | [diff] [blame] | 100 | self.controller.message_send(m) |
Ed Swierk | 47f608d | 2012-11-12 15:36:32 -0800 | [diff] [blame] | 101 | 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 Lane | 28fa927 | 2013-03-08 16:00:25 -0800 | [diff] [blame^] | 117 | m, r = self.controller.transact(ofp.message.features_request(), 2) |
Ed Swierk | 47f608d | 2012-11-12 15:36:32 -0800 | [diff] [blame] | 118 | 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 Lane | 28fa927 | 2013-03-08 16:00:25 -0800 | [diff] [blame^] | 127 | m, r = self.controller.transact(ofp.message.features_request(), 2) |
Ed Swierk | 47f608d | 2012-11-12 15:36:32 -0800 | [diff] [blame] | 128 | 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 Lane | 28fa927 | 2013-03-08 16:00:25 -0800 | [diff] [blame^] | 142 | act3 = ofp.action.action_output() |
Ed Swierk | 47f608d | 2012-11-12 15:36:32 -0800 | [diff] [blame] | 143 | act3.port = ports[1] |
Rich Lane | 28fa927 | 2013-03-08 16:00:25 -0800 | [diff] [blame^] | 144 | flow_mod = ofp.message.flow_mod() |
Ed Swierk | 47f608d | 2012-11-12 15:36:32 -0800 | [diff] [blame] | 145 | flow_mod.match = match |
Rich Lane | e30455b | 2013-01-03 16:24:44 -0800 | [diff] [blame] | 146 | flow_mod.actions.add(act1) |
| 147 | flow_mod.actions.add(act2) |
| 148 | flow_mod.actions.add(act3) |
Rich Lane | 32bf948 | 2013-01-03 17:26:30 -0800 | [diff] [blame] | 149 | delete_all_flows(self.controller) |
Rich Lane | 5c3151c | 2013-01-03 17:15:41 -0800 | [diff] [blame] | 150 | self.controller.message_send(flow_mod) |
Rich Lane | 3a261d5 | 2013-01-03 17:45:08 -0800 | [diff] [blame] | 151 | do_barrier(self.controller) |
Ed Swierk | 47f608d | 2012-11-12 15:36:32 -0800 | [diff] [blame] | 152 | |
| 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) |