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 |
| 9 | import oftest.cstruct as ofp |
| 10 | import oftest.message as message |
| 11 | import oftest.action as action |
| 12 | import oftest.action_list as action_list |
| 13 | import oftest.base_tests as base_tests |
| 14 | |
| 15 | from oftest.testutils import * |
| 16 | |
| 17 | class 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 | |
| 76 | action_list.action_object_map[ofp.OFPAT_VENDOR] = bsn_action_mirror |
| 77 | |
Rich Lane | 0a4f637 | 2013-01-02 14:40:22 -0800 | [diff] [blame] | 78 | @nonstandard |
Ed Swierk | 47f608d | 2012-11-12 15:36:32 -0800 | [diff] [blame] | 79 | class BSNMirrorAction(base_tests.SimpleDataPlane): |
| 80 | """ |
| 81 | Exercise BSN vendor extension for copying packets to a mirror destination |
| 82 | port |
| 83 | """ |
| 84 | |
Ed Swierk | 47f608d | 2012-11-12 15:36:32 -0800 | [diff] [blame] | 85 | 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 Lane | 5c3151c | 2013-01-03 17:15:41 -0800 | [diff] [blame] | 93 | self.controller.message_send(m) |
Ed Swierk | 47f608d | 2012-11-12 15:36:32 -0800 | [diff] [blame] | 94 | |
| 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 Lane | 5c3151c | 2013-01-03 17:15:41 -0800 | [diff] [blame] | 103 | self.controller.message_send(m) |
Ed Swierk | 47f608d | 2012-11-12 15:36:32 -0800 | [diff] [blame] | 104 | 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 Lane | e30455b | 2013-01-03 16:24:44 -0800 | [diff] [blame] | 149 | flow_mod.actions.add(act1) |
| 150 | flow_mod.actions.add(act2) |
| 151 | flow_mod.actions.add(act3) |
Rich Lane | 32bf948 | 2013-01-03 17:26:30 -0800 | [diff] [blame] | 152 | delete_all_flows(self.controller) |
Rich Lane | 5c3151c | 2013-01-03 17:15:41 -0800 | [diff] [blame] | 153 | self.controller.message_send(flow_mod) |
Rich Lane | 3a261d5 | 2013-01-03 17:45:08 -0800 | [diff] [blame] | 154 | do_barrier(self.controller) |
Ed Swierk | 47f608d | 2012-11-12 15:36:32 -0800 | [diff] [blame] | 155 | |
| 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) |