Dan Talayco | d2ca103 | 2010-03-10 14:40:26 -0800 | [diff] [blame] | 1 | import sys |
Dan Talayco | 92c9912 | 2010-06-03 13:53:18 -0700 | [diff] [blame] | 2 | import copy |
Rich Lane | 6242d9f | 2013-01-06 17:35:39 -0800 | [diff] [blame] | 3 | import logging |
| 4 | import types |
| 5 | import time |
Dan Talayco | d2ca103 | 2010-03-10 14:40:26 -0800 | [diff] [blame] | 6 | |
| 7 | try: |
| 8 | import scapy.all as scapy |
| 9 | except: |
| 10 | try: |
| 11 | import scapy as scapy |
| 12 | except: |
| 13 | sys.exit("Need to install scapy for packet parsing") |
Dan Talayco | 41eae8b | 2010-03-10 13:57:06 -0800 | [diff] [blame] | 14 | |
Rich Lane | cd97d3d | 2013-01-07 18:50:06 -0800 | [diff] [blame] | 15 | import oftest |
| 16 | import oftest.controller |
| 17 | import oftest.dataplane |
| 18 | import of10.cstruct |
| 19 | import of10.message |
| 20 | import of10.action |
| 21 | import of10.parse |
Dan Talayco | c901f4d | 2010-03-07 21:55:45 -0800 | [diff] [blame] | 22 | |
Dan Talayco | ba3745c | 2010-07-21 21:51:08 -0700 | [diff] [blame] | 23 | global skipped_test_count |
| 24 | skipped_test_count = 0 |
| 25 | |
Dan Talayco | 551befa | 2010-07-15 17:05:32 -0700 | [diff] [blame] | 26 | # Some useful defines |
| 27 | IP_ETHERTYPE = 0x800 |
| 28 | TCP_PROTOCOL = 0x6 |
| 29 | UDP_PROTOCOL = 0x11 |
| 30 | |
Jeffrey Townsend | 5cb7ed3 | 2012-08-17 18:11:01 +0000 | [diff] [blame] | 31 | MINSIZE = 0 |
| 32 | |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame] | 33 | def delete_all_flows(ctrl): |
Dan Talayco | 41eae8b | 2010-03-10 13:57:06 -0800 | [diff] [blame] | 34 | """ |
| 35 | Delete all flows on the switch |
| 36 | @param ctrl The controller object for the test |
Dan Talayco | 41eae8b | 2010-03-10 13:57:06 -0800 | [diff] [blame] | 37 | """ |
| 38 | |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame] | 39 | logging.info("Deleting all flows") |
Rich Lane | cd97d3d | 2013-01-07 18:50:06 -0800 | [diff] [blame] | 40 | msg = of10.message.flow_mod() |
| 41 | msg.match.wildcards = of10.cstruct.OFPFW_ALL |
| 42 | msg.out_port = of10.cstruct.OFPP_NONE |
| 43 | msg.command = of10.cstruct.OFPFC_DELETE |
Dan Talayco | c901f4d | 2010-03-07 21:55:45 -0800 | [diff] [blame] | 44 | msg.buffer_id = 0xffffffff |
Rich Lane | 5c3151c | 2013-01-03 17:15:41 -0800 | [diff] [blame] | 45 | ctrl.message_send(msg) |
Rich Lane | 32bf948 | 2013-01-03 17:26:30 -0800 | [diff] [blame] | 46 | return 0 # for backwards compatibility |
Dan Talayco | 41eae8b | 2010-03-10 13:57:06 -0800 | [diff] [blame] | 47 | |
Ed Swierk | 99a74de | 2012-08-22 06:40:54 -0700 | [diff] [blame] | 48 | def required_wildcards(parent): |
Rich Lane | 2014f9b | 2012-10-05 15:29:40 -0700 | [diff] [blame] | 49 | w = test_param_get('required_wildcards', default='default') |
Ed Swierk | 99a74de | 2012-08-22 06:40:54 -0700 | [diff] [blame] | 50 | if w == 'l3-l4': |
Rich Lane | cd97d3d | 2013-01-07 18:50:06 -0800 | [diff] [blame] | 51 | return (of10.cstruct.OFPFW_NW_SRC_ALL | of10.cstruct.OFPFW_NW_DST_ALL | of10.cstruct.OFPFW_NW_TOS |
| 52 | | of10.cstruct.OFPFW_NW_PROTO | of10.cstruct.OFPFW_TP_SRC | of10.cstruct.OFPFW_TP_DST) |
Ed Swierk | 99a74de | 2012-08-22 06:40:54 -0700 | [diff] [blame] | 53 | else: |
| 54 | return 0 |
| 55 | |
Dan Talayco | 41eae8b | 2010-03-10 13:57:06 -0800 | [diff] [blame] | 56 | def simple_tcp_packet(pktlen=100, |
| 57 | dl_dst='00:01:02:03:04:05', |
| 58 | dl_src='00:06:07:08:09:0a', |
Tatsuya Yabe | 460321e | 2010-05-25 17:50:49 -0700 | [diff] [blame] | 59 | dl_vlan_enable=False, |
| 60 | dl_vlan=0, |
| 61 | dl_vlan_pcp=0, |
Dan Talayco | 551befa | 2010-07-15 17:05:32 -0700 | [diff] [blame] | 62 | dl_vlan_cfi=0, |
Dan Talayco | 41eae8b | 2010-03-10 13:57:06 -0800 | [diff] [blame] | 63 | ip_src='192.168.0.1', |
| 64 | ip_dst='192.168.0.2', |
Tatsuya Yabe | 460321e | 2010-05-25 17:50:49 -0700 | [diff] [blame] | 65 | ip_tos=0, |
Dan Talayco | 41eae8b | 2010-03-10 13:57:06 -0800 | [diff] [blame] | 66 | tcp_sport=1234, |
Christian Dickmann | 8b59b4b | 2012-09-23 16:48:30 -0700 | [diff] [blame] | 67 | tcp_dport=80, |
| 68 | ip_ihl=None, |
| 69 | ip_options=False |
Dan Talayco | 41eae8b | 2010-03-10 13:57:06 -0800 | [diff] [blame] | 70 | ): |
| 71 | """ |
| 72 | Return a simple dataplane TCP packet |
| 73 | |
| 74 | Supports a few parameters: |
| 75 | @param len Length of packet in bytes w/o CRC |
| 76 | @param dl_dst Destinatino MAC |
| 77 | @param dl_src Source MAC |
Tatsuya Yabe | 460321e | 2010-05-25 17:50:49 -0700 | [diff] [blame] | 78 | @param dl_vlan_enable True if the packet is with vlan, False otherwise |
| 79 | @param dl_vlan VLAN ID |
| 80 | @param dl_vlan_pcp VLAN priority |
Dan Talayco | 41eae8b | 2010-03-10 13:57:06 -0800 | [diff] [blame] | 81 | @param ip_src IP source |
| 82 | @param ip_dst IP destination |
Tatsuya Yabe | 460321e | 2010-05-25 17:50:49 -0700 | [diff] [blame] | 83 | @param ip_tos IP ToS |
Dan Talayco | 41eae8b | 2010-03-10 13:57:06 -0800 | [diff] [blame] | 84 | @param tcp_dport TCP destination port |
| 85 | @param ip_sport TCP source port |
| 86 | |
| 87 | Generates a simple TCP request. Users |
| 88 | shouldn't assume anything about this packet other than that |
| 89 | it is a valid ethernet/IP/TCP frame. |
| 90 | """ |
Jeffrey Townsend | 5cb7ed3 | 2012-08-17 18:11:01 +0000 | [diff] [blame] | 91 | |
| 92 | if MINSIZE > pktlen: |
| 93 | pktlen = MINSIZE |
| 94 | |
Dan Talayco | 551befa | 2010-07-15 17:05:32 -0700 | [diff] [blame] | 95 | # Note Dot1Q.id is really CFI |
Shudong Zhou | 43ee54c | 2012-12-13 15:52:37 -0800 | [diff] [blame] | 96 | if (dl_vlan_enable): |
Tatsuya Yabe | 460321e | 2010-05-25 17:50:49 -0700 | [diff] [blame] | 97 | pkt = scapy.Ether(dst=dl_dst, src=dl_src)/ \ |
Dan Talayco | 551befa | 2010-07-15 17:05:32 -0700 | [diff] [blame] | 98 | scapy.Dot1Q(prio=dl_vlan_pcp, id=dl_vlan_cfi, vlan=dl_vlan)/ \ |
Christian Dickmann | 8b59b4b | 2012-09-23 16:48:30 -0700 | [diff] [blame] | 99 | scapy.IP(src=ip_src, dst=ip_dst, tos=ip_tos, ihl=ip_ihl)/ \ |
Tatsuya Yabe | 460321e | 2010-05-25 17:50:49 -0700 | [diff] [blame] | 100 | scapy.TCP(sport=tcp_sport, dport=tcp_dport) |
| 101 | else: |
Christian Dickmann | 8b59b4b | 2012-09-23 16:48:30 -0700 | [diff] [blame] | 102 | if not ip_options: |
| 103 | pkt = scapy.Ether(dst=dl_dst, src=dl_src)/ \ |
| 104 | scapy.IP(src=ip_src, dst=ip_dst, tos=ip_tos, ihl=ip_ihl)/ \ |
| 105 | scapy.TCP(sport=tcp_sport, dport=tcp_dport) |
| 106 | else: |
| 107 | pkt = scapy.Ether(dst=dl_dst, src=dl_src)/ \ |
| 108 | scapy.IP(src=ip_src, dst=ip_dst, tos=ip_tos, ihl=ip_ihl, options=ip_options)/ \ |
| 109 | scapy.TCP(sport=tcp_sport, dport=tcp_dport) |
Tatsuya Yabe | 460321e | 2010-05-25 17:50:49 -0700 | [diff] [blame] | 110 | |
Dan Talayco | 41eae8b | 2010-03-10 13:57:06 -0800 | [diff] [blame] | 111 | pkt = pkt/("D" * (pktlen - len(pkt))) |
| 112 | |
| 113 | return pkt |
| 114 | |
Rich Lane | 6ee7bea | 2012-10-26 16:19:29 -0700 | [diff] [blame] | 115 | def simple_udp_packet(pktlen=100, |
| 116 | dl_dst='00:01:02:03:04:05', |
| 117 | dl_src='00:06:07:08:09:0a', |
| 118 | dl_vlan_enable=False, |
| 119 | dl_vlan=0, |
| 120 | dl_vlan_pcp=0, |
| 121 | dl_vlan_cfi=0, |
| 122 | ip_src='192.168.0.1', |
| 123 | ip_dst='192.168.0.2', |
| 124 | ip_tos=0, |
| 125 | udp_sport=1234, |
| 126 | udp_dport=80, |
| 127 | ip_ihl=None, |
| 128 | ip_options=False |
| 129 | ): |
| 130 | """ |
| 131 | Return a simple dataplane UDP packet |
| 132 | |
| 133 | Supports a few parameters: |
| 134 | @param len Length of packet in bytes w/o CRC |
| 135 | @param dl_dst Destination MAC |
| 136 | @param dl_src Source MAC |
| 137 | @param dl_vlan_enable True if the packet is with vlan, False otherwise |
| 138 | @param dl_vlan VLAN ID |
| 139 | @param dl_vlan_pcp VLAN priority |
| 140 | @param ip_src IP source |
| 141 | @param ip_dst IP destination |
| 142 | @param ip_tos IP ToS |
| 143 | @param udp_dport UDP destination port |
| 144 | @param udp_sport UDP source port |
| 145 | |
| 146 | Generates a simple UDP packet. Users shouldn't assume anything about |
| 147 | this packet other than that it is a valid ethernet/IP/UDP frame. |
| 148 | """ |
| 149 | |
| 150 | if MINSIZE > pktlen: |
| 151 | pktlen = MINSIZE |
| 152 | |
| 153 | # Note Dot1Q.id is really CFI |
| 154 | if (dl_vlan_enable): |
| 155 | pkt = scapy.Ether(dst=dl_dst, src=dl_src)/ \ |
| 156 | scapy.Dot1Q(prio=dl_vlan_pcp, id=dl_vlan_cfi, vlan=dl_vlan)/ \ |
| 157 | scapy.IP(src=ip_src, dst=ip_dst, tos=ip_tos, ihl=ip_ihl)/ \ |
| 158 | scapy.UDP(sport=udp_sport, dport=udp_dport) |
| 159 | else: |
| 160 | if not ip_options: |
| 161 | pkt = scapy.Ether(dst=dl_dst, src=dl_src)/ \ |
| 162 | scapy.IP(src=ip_src, dst=ip_dst, tos=ip_tos, ihl=ip_ihl)/ \ |
| 163 | scapy.UDP(sport=udp_sport, dport=udp_dport) |
| 164 | else: |
| 165 | pkt = scapy.Ether(dst=dl_dst, src=dl_src)/ \ |
| 166 | scapy.IP(src=ip_src, dst=ip_dst, tos=ip_tos, ihl=ip_ihl, options=ip_options)/ \ |
| 167 | scapy.UDP(sport=udp_sport, dport=udp_dport) |
| 168 | |
| 169 | pkt = pkt/("D" * (pktlen - len(pkt))) |
| 170 | |
| 171 | return pkt |
| 172 | |
Tatsuya Yabe | b8fb3c3 | 2010-06-14 15:48:36 -0700 | [diff] [blame] | 173 | def simple_icmp_packet(pktlen=60, |
| 174 | dl_dst='00:01:02:03:04:05', |
| 175 | dl_src='00:06:07:08:09:0a', |
| 176 | dl_vlan_enable=False, |
| 177 | dl_vlan=0, |
| 178 | dl_vlan_pcp=0, |
| 179 | ip_src='192.168.0.1', |
| 180 | ip_dst='192.168.0.2', |
| 181 | ip_tos=0, |
| 182 | icmp_type=8, |
| 183 | icmp_code=0 |
| 184 | ): |
| 185 | """ |
| 186 | Return a simple ICMP packet |
| 187 | |
| 188 | Supports a few parameters: |
| 189 | @param len Length of packet in bytes w/o CRC |
| 190 | @param dl_dst Destinatino MAC |
| 191 | @param dl_src Source MAC |
| 192 | @param dl_vlan_enable True if the packet is with vlan, False otherwise |
| 193 | @param dl_vlan VLAN ID |
| 194 | @param dl_vlan_pcp VLAN priority |
| 195 | @param ip_src IP source |
| 196 | @param ip_dst IP destination |
| 197 | @param ip_tos IP ToS |
| 198 | @param icmp_type ICMP type |
| 199 | @param icmp_code ICMP code |
| 200 | |
| 201 | Generates a simple ICMP ECHO REQUEST. Users |
| 202 | shouldn't assume anything about this packet other than that |
| 203 | it is a valid ethernet/ICMP frame. |
| 204 | """ |
Jeffrey Townsend | 5cb7ed3 | 2012-08-17 18:11:01 +0000 | [diff] [blame] | 205 | |
| 206 | if MINSIZE > pktlen: |
| 207 | pktlen = MINSIZE |
| 208 | |
Tatsuya Yabe | b8fb3c3 | 2010-06-14 15:48:36 -0700 | [diff] [blame] | 209 | if (dl_vlan_enable): |
| 210 | pkt = scapy.Ether(dst=dl_dst, src=dl_src)/ \ |
| 211 | scapy.Dot1Q(prio=dl_vlan_pcp, id=0, vlan=dl_vlan)/ \ |
| 212 | scapy.IP(src=ip_src, dst=ip_dst, tos=ip_tos)/ \ |
| 213 | scapy.ICMP(type=icmp_type, code=icmp_code) |
| 214 | else: |
| 215 | pkt = scapy.Ether(dst=dl_dst, src=dl_src)/ \ |
| 216 | scapy.IP(src=ip_src, dst=ip_dst, tos=ip_tos)/ \ |
| 217 | scapy.ICMP(type=icmp_type, code=icmp_code) |
| 218 | |
| 219 | pkt = pkt/("0" * (pktlen - len(pkt))) |
| 220 | |
| 221 | return pkt |
| 222 | |
Ed Swierk | 0aeff8c | 2012-03-23 20:27:18 -0700 | [diff] [blame] | 223 | def simple_eth_packet(pktlen=60, |
| 224 | dl_dst='00:01:02:03:04:05', |
| 225 | dl_src='01:80:c2:00:00:00', |
| 226 | dl_type=0x88cc): |
Jeffrey Townsend | 5cb7ed3 | 2012-08-17 18:11:01 +0000 | [diff] [blame] | 227 | |
| 228 | if MINSIZE > pktlen: |
| 229 | pktlen = MINSIZE |
| 230 | |
Ed Swierk | 0aeff8c | 2012-03-23 20:27:18 -0700 | [diff] [blame] | 231 | pkt = scapy.Ether(dst=dl_dst, src=dl_src, type=dl_type) |
| 232 | |
| 233 | pkt = pkt/("0" * (pktlen - len(pkt))) |
| 234 | |
| 235 | return pkt |
| 236 | |
Shudong Zhou | 43ee54c | 2012-12-13 15:52:37 -0800 | [diff] [blame] | 237 | def qinq_tcp_packet(pktlen=100, |
| 238 | dl_dst='00:01:02:03:04:05', |
| 239 | dl_src='00:06:07:08:09:0a', |
| 240 | dl_vlan_outer=20, |
| 241 | dl_vlan_pcp_outer=0, |
| 242 | dl_vlan_cfi_outer=0, |
| 243 | dl_vlan=10, |
| 244 | dl_vlan_pcp=0, |
| 245 | dl_vlan_cfi=0, |
| 246 | ip_src='192.168.0.1', |
| 247 | ip_dst='192.168.0.2', |
| 248 | ip_tos=0, |
| 249 | tcp_sport=1234, |
| 250 | tcp_dport=80, |
| 251 | ip_ihl=None, |
| 252 | ip_options=False |
| 253 | ): |
| 254 | """ |
| 255 | Return a doubly tagged dataplane TCP packet |
| 256 | |
| 257 | Supports a few parameters: |
| 258 | @param len Length of packet in bytes w/o CRC |
| 259 | @param dl_dst Destinatino MAC |
| 260 | @param dl_src Source MAC |
| 261 | @param dl_vlan_outer Outer VLAN ID |
| 262 | @param dl_vlan_pcp_outer Outer VLAN priority |
| 263 | @param dl_vlan_cfi_outer Outer VLAN cfi bit |
| 264 | @param dl_vlan Inner VLAN ID |
| 265 | @param dl_vlan_pcp VLAN priority |
| 266 | @param dl_vlan_cfi VLAN cfi bit |
| 267 | @param ip_src IP source |
| 268 | @param ip_dst IP destination |
| 269 | @param ip_tos IP ToS |
| 270 | @param tcp_dport TCP destination port |
| 271 | @param ip_sport TCP source port |
| 272 | |
| 273 | Generates a TCP request. Users |
| 274 | shouldn't assume anything about this packet other than that |
| 275 | it is a valid ethernet/IP/TCP frame. |
| 276 | """ |
| 277 | |
| 278 | if MINSIZE > pktlen: |
| 279 | pktlen = MINSIZE |
| 280 | |
| 281 | # Note Dot1Q.id is really CFI |
| 282 | pkt = scapy.Ether(dst=dl_dst, src=dl_src)/ \ |
| 283 | scapy.Dot1Q(prio=dl_vlan_pcp_outer, id=dl_vlan_cfi_outer, vlan=dl_vlan_outer)/ \ |
| 284 | scapy.Dot1Q(prio=dl_vlan_pcp, id=dl_vlan_cfi, vlan=dl_vlan)/ \ |
| 285 | scapy.IP(src=ip_src, dst=ip_dst, tos=ip_tos, ihl=ip_ihl)/ \ |
| 286 | scapy.TCP(sport=tcp_sport, dport=tcp_dport) |
| 287 | |
| 288 | pkt = pkt/("D" * (pktlen - len(pkt))) |
| 289 | |
| 290 | return pkt |
| 291 | |
Shudong Zhou | b7f1246 | 2012-11-20 13:01:12 -0800 | [diff] [blame] | 292 | def do_barrier(ctrl, timeout=-1): |
Dan Talayco | 0fc08bd | 2012-04-09 16:56:18 -0700 | [diff] [blame] | 293 | """ |
| 294 | Do a barrier command |
| 295 | Return 0 on success, -1 on error |
| 296 | """ |
Rich Lane | cd97d3d | 2013-01-07 18:50:06 -0800 | [diff] [blame] | 297 | b = of10.message.barrier_request() |
Shudong Zhou | b7f1246 | 2012-11-20 13:01:12 -0800 | [diff] [blame] | 298 | (resp, pkt) = ctrl.transact(b, timeout=timeout) |
Rich Lane | 3a261d5 | 2013-01-03 17:45:08 -0800 | [diff] [blame] | 299 | if resp is None: |
| 300 | raise AssertionError("barrier failed") |
Dan Talayco | f6b9483 | 2012-04-12 21:50:57 -0700 | [diff] [blame] | 301 | # We'll trust the transaction processing in the controller that xid matched |
Rich Lane | 3a261d5 | 2013-01-03 17:45:08 -0800 | [diff] [blame] | 302 | return 0 # for backwards compatibility |
Dan Talayco | 92c9912 | 2010-06-03 13:53:18 -0700 | [diff] [blame] | 303 | |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame] | 304 | def port_config_get(controller, port_no): |
Dan Talayco | 92c9912 | 2010-06-03 13:53:18 -0700 | [diff] [blame] | 305 | """ |
| 306 | Get a port's configuration |
| 307 | |
| 308 | Gets the switch feature configuration and grabs one port's |
| 309 | configuration |
| 310 | |
| 311 | @returns (hwaddr, config, advert) The hwaddress, configuration and |
| 312 | advertised values |
| 313 | """ |
Rich Lane | cd97d3d | 2013-01-07 18:50:06 -0800 | [diff] [blame] | 314 | request = of10.message.features_request() |
Rich Lane | c8aaa3e | 2012-07-26 19:28:02 -0700 | [diff] [blame] | 315 | reply, pkt = controller.transact(request) |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame] | 316 | logging.debug(reply.show()) |
Dan Talayco | 92c9912 | 2010-06-03 13:53:18 -0700 | [diff] [blame] | 317 | if reply is None: |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame] | 318 | logging.warn("Get feature request failed") |
Dan Talayco | 92c9912 | 2010-06-03 13:53:18 -0700 | [diff] [blame] | 319 | return None, None, None |
| 320 | for idx in range(len(reply.ports)): |
| 321 | if reply.ports[idx].port_no == port_no: |
| 322 | return (reply.ports[idx].hw_addr, reply.ports[idx].config, |
| 323 | reply.ports[idx].advertised) |
| 324 | |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame] | 325 | logging.warn("Did not find port number for port config") |
Dan Talayco | 92c9912 | 2010-06-03 13:53:18 -0700 | [diff] [blame] | 326 | return None, None, None |
| 327 | |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame] | 328 | def port_config_set(controller, port_no, config, mask): |
Dan Talayco | 92c9912 | 2010-06-03 13:53:18 -0700 | [diff] [blame] | 329 | """ |
| 330 | Set the port configuration according the given parameters |
| 331 | |
| 332 | Gets the switch feature configuration and updates one port's |
| 333 | configuration value according to config and mask |
| 334 | """ |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame] | 335 | logging.info("Setting port " + str(port_no) + " to config " + str(config)) |
Rich Lane | cd97d3d | 2013-01-07 18:50:06 -0800 | [diff] [blame] | 336 | request = of10.message.features_request() |
Rich Lane | c8aaa3e | 2012-07-26 19:28:02 -0700 | [diff] [blame] | 337 | reply, pkt = controller.transact(request) |
Dan Talayco | 92c9912 | 2010-06-03 13:53:18 -0700 | [diff] [blame] | 338 | if reply is None: |
| 339 | return -1 |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame] | 340 | logging.debug(reply.show()) |
Ed Swierk | 8d84ce0 | 2012-12-27 15:36:48 -0800 | [diff] [blame] | 341 | p = None |
Dan Talayco | 92c9912 | 2010-06-03 13:53:18 -0700 | [diff] [blame] | 342 | for idx in range(len(reply.ports)): |
| 343 | if reply.ports[idx].port_no == port_no: |
Ed Swierk | 8d84ce0 | 2012-12-27 15:36:48 -0800 | [diff] [blame] | 344 | p = reply.ports[idx] |
Dan Talayco | 92c9912 | 2010-06-03 13:53:18 -0700 | [diff] [blame] | 345 | break |
Rich Lane | cd97d3d | 2013-01-07 18:50:06 -0800 | [diff] [blame] | 346 | mod = of10.message.port_mod() |
Dan Talayco | 92c9912 | 2010-06-03 13:53:18 -0700 | [diff] [blame] | 347 | mod.port_no = port_no |
Ed Swierk | 8d84ce0 | 2012-12-27 15:36:48 -0800 | [diff] [blame] | 348 | if p: |
| 349 | mod.hw_addr = p.hw_addr |
Dan Talayco | 92c9912 | 2010-06-03 13:53:18 -0700 | [diff] [blame] | 350 | mod.config = config |
| 351 | mod.mask = mask |
Ed Swierk | 8d84ce0 | 2012-12-27 15:36:48 -0800 | [diff] [blame] | 352 | if p: |
| 353 | mod.advertise = p.advertised |
Rich Lane | 5c3151c | 2013-01-03 17:15:41 -0800 | [diff] [blame] | 354 | controller.message_send(mod) |
| 355 | return 0 |
Dan Talayco | 92c9912 | 2010-06-03 13:53:18 -0700 | [diff] [blame] | 356 | |
Rich Lane | 2014f9b | 2012-10-05 15:29:40 -0700 | [diff] [blame] | 357 | def receive_pkt_check(dp, pkt, yes_ports, no_ports, assert_if): |
Dan Talayco | 92c9912 | 2010-06-03 13:53:18 -0700 | [diff] [blame] | 358 | """ |
| 359 | Check for proper receive packets across all ports |
Ken Chiang | 1bf0160 | 2012-04-04 10:48:23 -0700 | [diff] [blame] | 360 | @param dp The dataplane object |
Dan Talayco | 92c9912 | 2010-06-03 13:53:18 -0700 | [diff] [blame] | 361 | @param pkt Expected packet; may be None if yes_ports is empty |
| 362 | @param yes_ports Set or list of ports that should recieve packet |
| 363 | @param no_ports Set or list of ports that should not receive packet |
| 364 | @param assert_if Object that implements assertXXX |
| 365 | """ |
Rich Lane | 9176567 | 2012-12-06 16:33:04 -0800 | [diff] [blame] | 366 | |
| 367 | # Wait this long for packets that we don't expect to receive. |
| 368 | # 100ms is (rarely) too short for positive tests on slow |
| 369 | # switches but is definitely not too short for a negative test. |
| 370 | negative_timeout = 0.1 |
| 371 | |
Dan Talayco | cf26b7a | 2011-08-05 10:15:35 -0700 | [diff] [blame] | 372 | exp_pkt_arg = None |
Rich Lane | cd97d3d | 2013-01-07 18:50:06 -0800 | [diff] [blame] | 373 | if oftest.config["relax"]: |
Dan Talayco | cf26b7a | 2011-08-05 10:15:35 -0700 | [diff] [blame] | 374 | exp_pkt_arg = pkt |
| 375 | |
Dan Talayco | 92c9912 | 2010-06-03 13:53:18 -0700 | [diff] [blame] | 376 | for ofport in yes_ports: |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame] | 377 | logging.debug("Checking for pkt on port " + str(ofport)) |
Ken Chiang | 1bf0160 | 2012-04-04 10:48:23 -0700 | [diff] [blame] | 378 | (rcv_port, rcv_pkt, pkt_time) = dp.poll( |
Rich Lane | c8aaa3e | 2012-07-26 19:28:02 -0700 | [diff] [blame] | 379 | port_number=ofport, exp_pkt=exp_pkt_arg) |
Dan Talayco | 92c9912 | 2010-06-03 13:53:18 -0700 | [diff] [blame] | 380 | assert_if.assertTrue(rcv_pkt is not None, |
| 381 | "Did not receive pkt on " + str(ofport)) |
Rich Lane | cd97d3d | 2013-01-07 18:50:06 -0800 | [diff] [blame] | 382 | if not oftest.dataplane.match_exp_pkt(pkt, rcv_pkt): |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame] | 383 | logging.debug("Sent %s" % format_packet(pkt)) |
| 384 | logging.debug("Resp %s" % format_packet(rcv_pkt)) |
Rich Lane | cd97d3d | 2013-01-07 18:50:06 -0800 | [diff] [blame] | 385 | assert_if.assertTrue(oftest.dataplane.match_exp_pkt(pkt, rcv_pkt), |
Ken Chiang | 1bf0160 | 2012-04-04 10:48:23 -0700 | [diff] [blame] | 386 | "Response packet does not match send packet " + |
| 387 | "on port " + str(ofport)) |
Dan Talayco | 73f8401 | 2012-10-02 09:23:18 -0700 | [diff] [blame] | 388 | if len(no_ports) > 0: |
Rich Lane | 9176567 | 2012-12-06 16:33:04 -0800 | [diff] [blame] | 389 | time.sleep(negative_timeout) |
Dan Talayco | 92c9912 | 2010-06-03 13:53:18 -0700 | [diff] [blame] | 390 | for ofport in no_ports: |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame] | 391 | logging.debug("Negative check for pkt on port " + str(ofport)) |
Ken Chiang | 1bf0160 | 2012-04-04 10:48:23 -0700 | [diff] [blame] | 392 | (rcv_port, rcv_pkt, pkt_time) = dp.poll( |
Rich Lane | 9176567 | 2012-12-06 16:33:04 -0800 | [diff] [blame] | 393 | port_number=ofport, timeout=0, exp_pkt=exp_pkt_arg) |
Dan Talayco | 92c9912 | 2010-06-03 13:53:18 -0700 | [diff] [blame] | 394 | assert_if.assertTrue(rcv_pkt is None, |
| 395 | "Unexpected pkt on port " + str(ofport)) |
Dan Talayco | 551befa | 2010-07-15 17:05:32 -0700 | [diff] [blame] | 396 | |
| 397 | |
Dan Talayco | c948d0b | 2012-03-23 12:17:54 -0700 | [diff] [blame] | 398 | def receive_pkt_verify(parent, egr_ports, exp_pkt, ing_port): |
Dan Talayco | 551befa | 2010-07-15 17:05:32 -0700 | [diff] [blame] | 399 | """ |
| 400 | Receive a packet and verify it matches an expected value |
Dan Talayco | f6e76c0 | 2012-03-23 10:56:12 -0700 | [diff] [blame] | 401 | @param egr_port A single port or list of ports |
Dan Talayco | 551befa | 2010-07-15 17:05:32 -0700 | [diff] [blame] | 402 | |
| 403 | parent must implement dataplane, assertTrue and assertEqual |
| 404 | """ |
Dan Talayco | cf26b7a | 2011-08-05 10:15:35 -0700 | [diff] [blame] | 405 | exp_pkt_arg = None |
Rich Lane | cd97d3d | 2013-01-07 18:50:06 -0800 | [diff] [blame] | 406 | if oftest.config["relax"]: |
Dan Talayco | cf26b7a | 2011-08-05 10:15:35 -0700 | [diff] [blame] | 407 | exp_pkt_arg = exp_pkt |
| 408 | |
Dan Talayco | f6e76c0 | 2012-03-23 10:56:12 -0700 | [diff] [blame] | 409 | if type(egr_ports) == type([]): |
| 410 | egr_port_list = egr_ports |
| 411 | else: |
| 412 | egr_port_list = [egr_ports] |
Dan Talayco | cf26b7a | 2011-08-05 10:15:35 -0700 | [diff] [blame] | 413 | |
Dan Talayco | f6e76c0 | 2012-03-23 10:56:12 -0700 | [diff] [blame] | 414 | # Expect a packet from each port on egr port list |
| 415 | for egr_port in egr_port_list: |
Dan Talayco | d8ae758 | 2012-03-23 12:24:56 -0700 | [diff] [blame] | 416 | check_port = egr_port |
Rich Lane | cd97d3d | 2013-01-07 18:50:06 -0800 | [diff] [blame] | 417 | if egr_port == of10.cstruct.OFPP_IN_PORT: |
Dan Talayco | d8ae758 | 2012-03-23 12:24:56 -0700 | [diff] [blame] | 418 | check_port = ing_port |
Dan Talayco | f6e76c0 | 2012-03-23 10:56:12 -0700 | [diff] [blame] | 419 | (rcv_port, rcv_pkt, pkt_time) = parent.dataplane.poll( |
Rich Lane | c8aaa3e | 2012-07-26 19:28:02 -0700 | [diff] [blame] | 420 | port_number=check_port, exp_pkt=exp_pkt_arg) |
Dan Talayco | 551befa | 2010-07-15 17:05:32 -0700 | [diff] [blame] | 421 | |
Dan Talayco | f6e76c0 | 2012-03-23 10:56:12 -0700 | [diff] [blame] | 422 | if rcv_pkt is None: |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame] | 423 | logging.error("ERROR: No packet received from " + |
Dan Talayco | d8ae758 | 2012-03-23 12:24:56 -0700 | [diff] [blame] | 424 | str(check_port)) |
Dan Talayco | 551befa | 2010-07-15 17:05:32 -0700 | [diff] [blame] | 425 | |
Dan Talayco | f6e76c0 | 2012-03-23 10:56:12 -0700 | [diff] [blame] | 426 | parent.assertTrue(rcv_pkt is not None, |
Dan Talayco | d8ae758 | 2012-03-23 12:24:56 -0700 | [diff] [blame] | 427 | "Did not receive packet port " + str(check_port)) |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame] | 428 | logging.debug("Packet len " + str(len(rcv_pkt)) + " in on " + |
Dan Talayco | f6e76c0 | 2012-03-23 10:56:12 -0700 | [diff] [blame] | 429 | str(rcv_port)) |
| 430 | |
| 431 | if str(exp_pkt) != str(rcv_pkt): |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame] | 432 | logging.error("ERROR: Packet match failed.") |
| 433 | logging.debug("Expected len " + str(len(exp_pkt)) + ": " |
Dan Talayco | f6e76c0 | 2012-03-23 10:56:12 -0700 | [diff] [blame] | 434 | + str(exp_pkt).encode('hex')) |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame] | 435 | logging.debug("Received len " + str(len(rcv_pkt)) + ": " |
Dan Talayco | f6e76c0 | 2012-03-23 10:56:12 -0700 | [diff] [blame] | 436 | + str(rcv_pkt).encode('hex')) |
Rich Lane | 5d7e89a | 2012-10-26 16:43:13 -0700 | [diff] [blame] | 437 | logging.debug("Expected packet: " + inspect_packet(scapy.Ether(str(exp_pkt)))) |
| 438 | logging.debug("Received packet: " + inspect_packet(scapy.Ether(str(rcv_pkt)))) |
Dan Talayco | f6e76c0 | 2012-03-23 10:56:12 -0700 | [diff] [blame] | 439 | parent.assertEqual(str(exp_pkt), str(rcv_pkt), |
Dan Talayco | d8ae758 | 2012-03-23 12:24:56 -0700 | [diff] [blame] | 440 | "Packet match error on port " + str(check_port)) |
Dan Talayco | f6e76c0 | 2012-03-23 10:56:12 -0700 | [diff] [blame] | 441 | |
Dan Talayco | 551befa | 2010-07-15 17:05:32 -0700 | [diff] [blame] | 442 | def match_verify(parent, req_match, res_match): |
| 443 | """ |
| 444 | Verify flow matches agree; if they disagree, report where |
| 445 | |
| 446 | parent must implement assertEqual |
| 447 | Use str() to ensure content is compared and not pointers |
| 448 | """ |
| 449 | |
| 450 | parent.assertEqual(req_match.wildcards, res_match.wildcards, |
| 451 | 'Match failed: wildcards: ' + hex(req_match.wildcards) + |
| 452 | " != " + hex(res_match.wildcards)) |
| 453 | parent.assertEqual(req_match.in_port, res_match.in_port, |
| 454 | 'Match failed: in_port: ' + str(req_match.in_port) + |
| 455 | " != " + str(res_match.in_port)) |
| 456 | parent.assertEqual(str(req_match.dl_src), str(res_match.dl_src), |
| 457 | 'Match failed: dl_src: ' + str(req_match.dl_src) + |
| 458 | " != " + str(res_match.dl_src)) |
| 459 | parent.assertEqual(str(req_match.dl_dst), str(res_match.dl_dst), |
| 460 | 'Match failed: dl_dst: ' + str(req_match.dl_dst) + |
| 461 | " != " + str(res_match.dl_dst)) |
| 462 | parent.assertEqual(req_match.dl_vlan, res_match.dl_vlan, |
| 463 | 'Match failed: dl_vlan: ' + str(req_match.dl_vlan) + |
| 464 | " != " + str(res_match.dl_vlan)) |
| 465 | parent.assertEqual(req_match.dl_vlan_pcp, res_match.dl_vlan_pcp, |
| 466 | 'Match failed: dl_vlan_pcp: ' + |
| 467 | str(req_match.dl_vlan_pcp) + " != " + |
| 468 | str(res_match.dl_vlan_pcp)) |
| 469 | parent.assertEqual(req_match.dl_type, res_match.dl_type, |
| 470 | 'Match failed: dl_type: ' + str(req_match.dl_type) + |
| 471 | " != " + str(res_match.dl_type)) |
| 472 | |
Rich Lane | cd97d3d | 2013-01-07 18:50:06 -0800 | [diff] [blame] | 473 | if (not(req_match.wildcards & of10.cstruct.OFPFW_DL_TYPE) |
Dan Talayco | 551befa | 2010-07-15 17:05:32 -0700 | [diff] [blame] | 474 | and (req_match.dl_type == IP_ETHERTYPE)): |
| 475 | parent.assertEqual(req_match.nw_tos, res_match.nw_tos, |
| 476 | 'Match failed: nw_tos: ' + str(req_match.nw_tos) + |
| 477 | " != " + str(res_match.nw_tos)) |
| 478 | parent.assertEqual(req_match.nw_proto, res_match.nw_proto, |
| 479 | 'Match failed: nw_proto: ' + str(req_match.nw_proto) + |
| 480 | " != " + str(res_match.nw_proto)) |
| 481 | parent.assertEqual(req_match.nw_src, res_match.nw_src, |
| 482 | 'Match failed: nw_src: ' + str(req_match.nw_src) + |
| 483 | " != " + str(res_match.nw_src)) |
| 484 | parent.assertEqual(req_match.nw_dst, res_match.nw_dst, |
| 485 | 'Match failed: nw_dst: ' + str(req_match.nw_dst) + |
| 486 | " != " + str(res_match.nw_dst)) |
| 487 | |
Rich Lane | cd97d3d | 2013-01-07 18:50:06 -0800 | [diff] [blame] | 488 | if (not(req_match.wildcards & of10.cstruct.OFPFW_NW_PROTO) |
Dan Talayco | 551befa | 2010-07-15 17:05:32 -0700 | [diff] [blame] | 489 | and ((req_match.nw_proto == TCP_PROTOCOL) |
| 490 | or (req_match.nw_proto == UDP_PROTOCOL))): |
| 491 | parent.assertEqual(req_match.tp_src, res_match.tp_src, |
| 492 | 'Match failed: tp_src: ' + |
| 493 | str(req_match.tp_src) + |
| 494 | " != " + str(res_match.tp_src)) |
| 495 | parent.assertEqual(req_match.tp_dst, res_match.tp_dst, |
| 496 | 'Match failed: tp_dst: ' + |
| 497 | str(req_match.tp_dst) + |
| 498 | " != " + str(res_match.tp_dst)) |
| 499 | |
Ed Swierk | 99a74de | 2012-08-22 06:40:54 -0700 | [diff] [blame] | 500 | def packet_to_flow_match(parent, packet): |
Rich Lane | cd97d3d | 2013-01-07 18:50:06 -0800 | [diff] [blame] | 501 | match = of10.parse.packet_to_flow_match(packet) |
Ed Swierk | 99a74de | 2012-08-22 06:40:54 -0700 | [diff] [blame] | 502 | match.wildcards |= required_wildcards(parent) |
| 503 | return match |
| 504 | |
| 505 | def flow_msg_create(parent, pkt, ing_port=None, action_list=None, wildcards=None, |
Dan Talayco | f6e76c0 | 2012-03-23 10:56:12 -0700 | [diff] [blame] | 506 | egr_ports=None, egr_queue=None, check_expire=False, in_band=False): |
Dan Talayco | 551befa | 2010-07-15 17:05:32 -0700 | [diff] [blame] | 507 | """ |
| 508 | Create a flow message |
| 509 | |
| 510 | Match on packet with given wildcards. |
| 511 | See flow_match_test for other parameter descriptoins |
| 512 | @param egr_queue if not None, make the output an enqueue action |
Dan Talayco | 677c0b7 | 2011-08-23 22:53:38 -0700 | [diff] [blame] | 513 | @param in_band if True, do not wildcard ingress port |
Dan Talayco | f6e76c0 | 2012-03-23 10:56:12 -0700 | [diff] [blame] | 514 | @param egr_ports None (drop), single port or list of ports |
Dan Talayco | 551befa | 2010-07-15 17:05:32 -0700 | [diff] [blame] | 515 | """ |
Rich Lane | cd97d3d | 2013-01-07 18:50:06 -0800 | [diff] [blame] | 516 | match = of10.parse.packet_to_flow_match(pkt) |
Dan Talayco | 551befa | 2010-07-15 17:05:32 -0700 | [diff] [blame] | 517 | parent.assertTrue(match is not None, "Flow match from pkt failed") |
Ed Swierk | 99a74de | 2012-08-22 06:40:54 -0700 | [diff] [blame] | 518 | if wildcards is None: |
| 519 | wildcards = required_wildcards(parent) |
Dan Talayco | 677c0b7 | 2011-08-23 22:53:38 -0700 | [diff] [blame] | 520 | if in_band: |
Rich Lane | cd97d3d | 2013-01-07 18:50:06 -0800 | [diff] [blame] | 521 | wildcards &= ~of10.cstruct.OFPFW_IN_PORT |
Dan Talayco | 551befa | 2010-07-15 17:05:32 -0700 | [diff] [blame] | 522 | match.wildcards = wildcards |
| 523 | match.in_port = ing_port |
| 524 | |
Dan Talayco | f6e76c0 | 2012-03-23 10:56:12 -0700 | [diff] [blame] | 525 | if type(egr_ports) == type([]): |
| 526 | egr_port_list = egr_ports |
| 527 | else: |
| 528 | egr_port_list = [egr_ports] |
| 529 | |
Rich Lane | cd97d3d | 2013-01-07 18:50:06 -0800 | [diff] [blame] | 530 | request = of10.message.flow_mod() |
Dan Talayco | 551befa | 2010-07-15 17:05:32 -0700 | [diff] [blame] | 531 | request.match = match |
| 532 | request.buffer_id = 0xffffffff |
| 533 | if check_expire: |
Rich Lane | cd97d3d | 2013-01-07 18:50:06 -0800 | [diff] [blame] | 534 | request.flags |= of10.cstruct.OFPFF_SEND_FLOW_REM |
Dan Talayco | 551befa | 2010-07-15 17:05:32 -0700 | [diff] [blame] | 535 | request.hard_timeout = 1 |
| 536 | |
| 537 | if action_list is not None: |
| 538 | for act in action_list: |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame] | 539 | logging.debug("Adding action " + act.show()) |
Rich Lane | e30455b | 2013-01-03 16:24:44 -0800 | [diff] [blame] | 540 | request.actions.add(act) |
Dan Talayco | 551befa | 2010-07-15 17:05:32 -0700 | [diff] [blame] | 541 | |
| 542 | # Set up output/enqueue action if directed |
| 543 | if egr_queue is not None: |
Dan Talayco | f6e76c0 | 2012-03-23 10:56:12 -0700 | [diff] [blame] | 544 | parent.assertTrue(egr_ports is not None, "Egress port not set") |
Rich Lane | cd97d3d | 2013-01-07 18:50:06 -0800 | [diff] [blame] | 545 | act = of10.action.action_enqueue() |
Dan Talayco | f6e76c0 | 2012-03-23 10:56:12 -0700 | [diff] [blame] | 546 | for egr_port in egr_port_list: |
| 547 | act.port = egr_port |
| 548 | act.queue_id = egr_queue |
Rich Lane | e30455b | 2013-01-03 16:24:44 -0800 | [diff] [blame] | 549 | request.actions.add(act) |
Dan Talayco | f6e76c0 | 2012-03-23 10:56:12 -0700 | [diff] [blame] | 550 | elif egr_ports is not None: |
| 551 | for egr_port in egr_port_list: |
Rich Lane | cd97d3d | 2013-01-07 18:50:06 -0800 | [diff] [blame] | 552 | act = of10.action.action_output() |
Dan Talayco | f6e76c0 | 2012-03-23 10:56:12 -0700 | [diff] [blame] | 553 | act.port = egr_port |
Rich Lane | e30455b | 2013-01-03 16:24:44 -0800 | [diff] [blame] | 554 | request.actions.add(act) |
Dan Talayco | 551befa | 2010-07-15 17:05:32 -0700 | [diff] [blame] | 555 | |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame] | 556 | logging.debug(request.show()) |
Dan Talayco | 551befa | 2010-07-15 17:05:32 -0700 | [diff] [blame] | 557 | |
| 558 | return request |
| 559 | |
Jeffrey Townsend | f3eae9c | 2012-03-28 18:23:21 -0700 | [diff] [blame] | 560 | def flow_msg_install(parent, request, clear_table_override=None): |
Dan Talayco | 551befa | 2010-07-15 17:05:32 -0700 | [diff] [blame] | 561 | """ |
| 562 | Install a flow mod message in the switch |
| 563 | |
| 564 | @param parent Must implement controller, assertEqual, assertTrue |
| 565 | @param request The request, all set to go |
| 566 | @param clear_table If true, clear the flow table before installing |
| 567 | """ |
Dan Talayco | 8a64e33 | 2012-03-28 14:53:20 -0700 | [diff] [blame] | 568 | |
Rich Lane | 2014f9b | 2012-10-05 15:29:40 -0700 | [diff] [blame] | 569 | clear_table = test_param_get('clear_table', default=True) |
Jeffrey Townsend | f3eae9c | 2012-03-28 18:23:21 -0700 | [diff] [blame] | 570 | if(clear_table_override != None): |
| 571 | clear_table = clear_table_override |
| 572 | |
| 573 | if clear_table: |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame] | 574 | logging.debug("Clear flow table") |
Rich Lane | 32bf948 | 2013-01-03 17:26:30 -0800 | [diff] [blame] | 575 | delete_all_flows(parent.controller) |
Dan Talayco | 551befa | 2010-07-15 17:05:32 -0700 | [diff] [blame] | 576 | |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame] | 577 | logging.debug("Insert flow") |
Rich Lane | 5c3151c | 2013-01-03 17:15:41 -0800 | [diff] [blame] | 578 | parent.controller.message_send(request) |
Rich Lane | e912d03 | 2012-12-22 14:28:33 -0800 | [diff] [blame] | 579 | |
Rich Lane | 3a261d5 | 2013-01-03 17:45:08 -0800 | [diff] [blame] | 580 | do_barrier(parent.controller) |
Dan Talayco | 551befa | 2010-07-15 17:05:32 -0700 | [diff] [blame] | 581 | |
Ed Swierk | 99a74de | 2012-08-22 06:40:54 -0700 | [diff] [blame] | 582 | def flow_match_test_port_pair(parent, ing_port, egr_ports, wildcards=None, |
Dan Talayco | 551befa | 2010-07-15 17:05:32 -0700 | [diff] [blame] | 583 | dl_vlan=-1, pkt=None, exp_pkt=None, |
Rich Lane | e5779d3 | 2012-10-05 17:56:04 -0700 | [diff] [blame] | 584 | action_list=None): |
Dan Talayco | 551befa | 2010-07-15 17:05:32 -0700 | [diff] [blame] | 585 | """ |
| 586 | Flow match test on single TCP packet |
Dan Talayco | f6e76c0 | 2012-03-23 10:56:12 -0700 | [diff] [blame] | 587 | @param egr_ports A single port or list of ports |
Dan Talayco | 551befa | 2010-07-15 17:05:32 -0700 | [diff] [blame] | 588 | |
| 589 | Run test with packet through switch from ing_port to egr_port |
| 590 | See flow_match_test for parameter descriptions |
| 591 | """ |
| 592 | |
Ed Swierk | 99a74de | 2012-08-22 06:40:54 -0700 | [diff] [blame] | 593 | if wildcards is None: |
| 594 | wildcards = required_wildcards(parent) |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame] | 595 | logging.info("Pkt match test: " + str(ing_port) + " to " + |
Dan Talayco | f6e76c0 | 2012-03-23 10:56:12 -0700 | [diff] [blame] | 596 | str(egr_ports)) |
Rich Lane | e5779d3 | 2012-10-05 17:56:04 -0700 | [diff] [blame] | 597 | logging.debug(" WC: " + hex(wildcards) + " vlan: " + str(dl_vlan)) |
Dan Talayco | 551befa | 2010-07-15 17:05:32 -0700 | [diff] [blame] | 598 | if pkt is None: |
| 599 | pkt = simple_tcp_packet(dl_vlan_enable=(dl_vlan >= 0), dl_vlan=dl_vlan) |
| 600 | |
| 601 | request = flow_msg_create(parent, pkt, ing_port=ing_port, |
Dan Talayco | f6e76c0 | 2012-03-23 10:56:12 -0700 | [diff] [blame] | 602 | wildcards=wildcards, egr_ports=egr_ports, |
Dan Talayco | 551befa | 2010-07-15 17:05:32 -0700 | [diff] [blame] | 603 | action_list=action_list) |
| 604 | |
| 605 | flow_msg_install(parent, request) |
| 606 | |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame] | 607 | logging.debug("Send packet: " + str(ing_port) + " to " + |
Dan Talayco | f6e76c0 | 2012-03-23 10:56:12 -0700 | [diff] [blame] | 608 | str(egr_ports)) |
Dan Talayco | 551befa | 2010-07-15 17:05:32 -0700 | [diff] [blame] | 609 | parent.dataplane.send(ing_port, str(pkt)) |
| 610 | |
| 611 | if exp_pkt is None: |
| 612 | exp_pkt = pkt |
Dan Talayco | c948d0b | 2012-03-23 12:17:54 -0700 | [diff] [blame] | 613 | receive_pkt_verify(parent, egr_ports, exp_pkt, ing_port) |
Dan Talayco | 551befa | 2010-07-15 17:05:32 -0700 | [diff] [blame] | 614 | |
Rich Lane | 89725bb | 2012-12-03 16:23:27 -0800 | [diff] [blame] | 615 | def flow_match_test_pktout(parent, ing_port, egr_ports, |
| 616 | dl_vlan=-1, pkt=None, exp_pkt=None, |
| 617 | action_list=None): |
| 618 | """ |
| 619 | Packet-out test on single TCP packet |
| 620 | @param egr_ports A single port or list of ports |
| 621 | |
| 622 | Run test sending packet-out to egr_ports. The goal is to test the actions |
| 623 | taken on the packet, not the matching which is of course irrelevant. |
| 624 | See flow_match_test for parameter descriptions |
| 625 | """ |
| 626 | |
| 627 | if pkt is None: |
| 628 | pkt = simple_tcp_packet(dl_vlan_enable=(dl_vlan >= 0), dl_vlan=dl_vlan) |
| 629 | |
Rich Lane | cd97d3d | 2013-01-07 18:50:06 -0800 | [diff] [blame] | 630 | msg = of10.message.packet_out() |
Rich Lane | 89725bb | 2012-12-03 16:23:27 -0800 | [diff] [blame] | 631 | msg.in_port = ing_port |
| 632 | msg.data = str(pkt) |
| 633 | if action_list is not None: |
| 634 | for act in action_list: |
Rich Lane | e30455b | 2013-01-03 16:24:44 -0800 | [diff] [blame] | 635 | msg.actions.add(act) |
Rich Lane | 89725bb | 2012-12-03 16:23:27 -0800 | [diff] [blame] | 636 | |
| 637 | # Set up output action |
| 638 | if egr_ports is not None: |
| 639 | for egr_port in egr_ports: |
Rich Lane | cd97d3d | 2013-01-07 18:50:06 -0800 | [diff] [blame] | 640 | act = of10.action.action_output() |
Rich Lane | 89725bb | 2012-12-03 16:23:27 -0800 | [diff] [blame] | 641 | act.port = egr_port |
Rich Lane | e30455b | 2013-01-03 16:24:44 -0800 | [diff] [blame] | 642 | msg.actions.add(act) |
Rich Lane | 89725bb | 2012-12-03 16:23:27 -0800 | [diff] [blame] | 643 | |
| 644 | logging.debug(msg.show()) |
Rich Lane | 5c3151c | 2013-01-03 17:15:41 -0800 | [diff] [blame] | 645 | parent.controller.message_send(msg) |
Rich Lane | 89725bb | 2012-12-03 16:23:27 -0800 | [diff] [blame] | 646 | |
| 647 | if exp_pkt is None: |
| 648 | exp_pkt = pkt |
| 649 | receive_pkt_verify(parent, egr_ports, exp_pkt, ing_port) |
| 650 | |
Dan Talayco | f6e76c0 | 2012-03-23 10:56:12 -0700 | [diff] [blame] | 651 | def get_egr_list(parent, of_ports, how_many, exclude_list=[]): |
| 652 | """ |
| 653 | Generate a list of ports avoiding those in the exclude list |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame] | 654 | @param parent Supplies logging |
Dan Talayco | f6e76c0 | 2012-03-23 10:56:12 -0700 | [diff] [blame] | 655 | @param of_ports List of OF port numbers |
| 656 | @param how_many Number of ports to be added to the list |
| 657 | @param exclude_list List of ports not to be used |
| 658 | @returns An empty list if unable to find enough ports |
| 659 | """ |
| 660 | |
Dan Talayco | c948d0b | 2012-03-23 12:17:54 -0700 | [diff] [blame] | 661 | if how_many == 0: |
| 662 | return [] |
| 663 | |
Dan Talayco | f6e76c0 | 2012-03-23 10:56:12 -0700 | [diff] [blame] | 664 | count = 0 |
| 665 | egr_ports = [] |
| 666 | for egr_idx in range(len(of_ports)): |
| 667 | if of_ports[egr_idx] not in exclude_list: |
| 668 | egr_ports.append(of_ports[egr_idx]) |
| 669 | count += 1 |
| 670 | if count >= how_many: |
| 671 | return egr_ports |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame] | 672 | logging.debug("Could not generate enough egress ports for test") |
Dan Talayco | f6e76c0 | 2012-03-23 10:56:12 -0700 | [diff] [blame] | 673 | return [] |
| 674 | |
Ed Swierk | 99a74de | 2012-08-22 06:40:54 -0700 | [diff] [blame] | 675 | def flow_match_test(parent, port_map, wildcards=None, dl_vlan=-1, pkt=None, |
Rich Lane | e5779d3 | 2012-10-05 17:56:04 -0700 | [diff] [blame] | 676 | exp_pkt=None, action_list=None, |
Dan Talayco | c948d0b | 2012-03-23 12:17:54 -0700 | [diff] [blame] | 677 | max_test=0, egr_count=1, ing_port=False): |
Dan Talayco | 551befa | 2010-07-15 17:05:32 -0700 | [diff] [blame] | 678 | """ |
Rich Lane | 89725bb | 2012-12-03 16:23:27 -0800 | [diff] [blame] | 679 | Run flow_match_test_port_pair on all port pairs and packet-out |
Dan Talayco | 551befa | 2010-07-15 17:05:32 -0700 | [diff] [blame] | 680 | |
| 681 | @param max_test If > 0 no more than this number of tests are executed. |
| 682 | @param parent Must implement controller, dataplane, assertTrue, assertEqual |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame] | 683 | and logging |
Dan Talayco | 551befa | 2010-07-15 17:05:32 -0700 | [diff] [blame] | 684 | @param pkt If not None, use this packet for ingress |
| 685 | @param wildcards For flow match entry |
Dan Talayco | 7918422 | 2010-11-01 12:24:29 -0700 | [diff] [blame] | 686 | @param dl_vlan If not -1, and pkt is None, create a pkt w/ VLAN tag |
Dan Talayco | 551befa | 2010-07-15 17:05:32 -0700 | [diff] [blame] | 687 | @param exp_pkt If not None, use this as the expected output pkt; els use pkt |
| 688 | @param action_list Additional actions to add to flow mod |
Dan Talayco | cfa172f | 2012-03-23 12:03:00 -0700 | [diff] [blame] | 689 | @param egr_count Number of egress ports; -1 means get from config w/ dflt 2 |
Dan Talayco | 551befa | 2010-07-15 17:05:32 -0700 | [diff] [blame] | 690 | """ |
Ed Swierk | 99a74de | 2012-08-22 06:40:54 -0700 | [diff] [blame] | 691 | if wildcards is None: |
| 692 | wildcards = required_wildcards(parent) |
Dan Talayco | 551befa | 2010-07-15 17:05:32 -0700 | [diff] [blame] | 693 | of_ports = port_map.keys() |
| 694 | of_ports.sort() |
| 695 | parent.assertTrue(len(of_ports) > 1, "Not enough ports for test") |
| 696 | test_count = 0 |
| 697 | |
Dan Talayco | cfa172f | 2012-03-23 12:03:00 -0700 | [diff] [blame] | 698 | if egr_count == -1: |
Rich Lane | 2014f9b | 2012-10-05 15:29:40 -0700 | [diff] [blame] | 699 | egr_count = test_param_get('egr_count', default=2) |
Dan Talayco | cfa172f | 2012-03-23 12:03:00 -0700 | [diff] [blame] | 700 | |
Dan Talayco | 551befa | 2010-07-15 17:05:32 -0700 | [diff] [blame] | 701 | for ing_idx in range(len(of_ports)): |
| 702 | ingress_port = of_ports[ing_idx] |
Dan Talayco | f6e76c0 | 2012-03-23 10:56:12 -0700 | [diff] [blame] | 703 | egr_ports = get_egr_list(parent, of_ports, egr_count, |
| 704 | exclude_list=[ingress_port]) |
Dan Talayco | c948d0b | 2012-03-23 12:17:54 -0700 | [diff] [blame] | 705 | if ing_port: |
Rich Lane | cd97d3d | 2013-01-07 18:50:06 -0800 | [diff] [blame] | 706 | egr_ports.append(of10.cstruct.OFPP_IN_PORT) |
Dan Talayco | f6e76c0 | 2012-03-23 10:56:12 -0700 | [diff] [blame] | 707 | if len(egr_ports) == 0: |
| 708 | parent.assertTrue(0, "Failed to generate egress port list") |
| 709 | |
| 710 | flow_match_test_port_pair(parent, ingress_port, egr_ports, |
| 711 | wildcards=wildcards, dl_vlan=dl_vlan, |
| 712 | pkt=pkt, exp_pkt=exp_pkt, |
Rich Lane | e5779d3 | 2012-10-05 17:56:04 -0700 | [diff] [blame] | 713 | action_list=action_list) |
Dan Talayco | f6e76c0 | 2012-03-23 10:56:12 -0700 | [diff] [blame] | 714 | test_count += 1 |
| 715 | if (max_test > 0) and (test_count > max_test): |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame] | 716 | logging.info("Ran " + str(test_count) + " tests; exiting") |
Rich Lane | 89725bb | 2012-12-03 16:23:27 -0800 | [diff] [blame] | 717 | break |
| 718 | |
Ed Swierk | 38eea08 | 2013-01-02 19:46:20 -0800 | [diff] [blame] | 719 | if not test_param_get('pktout_actions', default=True): |
| 720 | return |
Rich Lane | 89725bb | 2012-12-03 16:23:27 -0800 | [diff] [blame] | 721 | |
| 722 | ingress_port = of_ports[0] |
| 723 | egr_ports = get_egr_list(parent, of_ports, egr_count, |
| 724 | exclude_list=[ingress_port]) |
| 725 | if ing_port: |
Rich Lane | cd97d3d | 2013-01-07 18:50:06 -0800 | [diff] [blame] | 726 | egr_ports.append(of10.cstruct.OFPP_IN_PORT) |
Rich Lane | 89725bb | 2012-12-03 16:23:27 -0800 | [diff] [blame] | 727 | flow_match_test_pktout(parent, ingress_port, egr_ports, |
| 728 | dl_vlan=dl_vlan, |
| 729 | pkt=pkt, exp_pkt=exp_pkt, |
| 730 | action_list=action_list) |
Dan Talayco | 551befa | 2010-07-15 17:05:32 -0700 | [diff] [blame] | 731 | |
Rich Lane | 2014f9b | 2012-10-05 15:29:40 -0700 | [diff] [blame] | 732 | def test_param_get(key, default=None): |
Dan Talayco | 4b2bee6 | 2010-07-20 14:10:05 -0700 | [diff] [blame] | 733 | """ |
| 734 | Return value passed via test-params if present |
| 735 | |
Dan Talayco | 4b2bee6 | 2010-07-20 14:10:05 -0700 | [diff] [blame] | 736 | @param key The lookup key |
| 737 | @param default Default value to use if not found |
| 738 | |
| 739 | If the pair 'key=val' appeared in the string passed to --test-params |
| 740 | on the command line, return val (as interpreted by exec). Otherwise |
| 741 | return default value. |
Dan Talayco | f6e76c0 | 2012-03-23 10:56:12 -0700 | [diff] [blame] | 742 | |
| 743 | WARNING: TEST PARAMETERS MUST BE PYTHON IDENTIFIERS; |
| 744 | eg egr_count, not egr-count. |
Dan Talayco | 4b2bee6 | 2010-07-20 14:10:05 -0700 | [diff] [blame] | 745 | """ |
| 746 | try: |
Rich Lane | cd97d3d | 2013-01-07 18:50:06 -0800 | [diff] [blame] | 747 | exec oftest.config["test_params"] |
Dan Talayco | 4b2bee6 | 2010-07-20 14:10:05 -0700 | [diff] [blame] | 748 | except: |
| 749 | return default |
| 750 | |
| 751 | s = "val = " + str(key) |
| 752 | try: |
| 753 | exec s |
| 754 | return val |
| 755 | except: |
| 756 | return default |
| 757 | |
| 758 | def action_generate(parent, field_to_mod, mod_field_vals): |
| 759 | """ |
| 760 | Create an action to modify the field indicated in field_to_mod |
| 761 | |
| 762 | @param parent Must implement, assertTrue |
| 763 | @param field_to_mod The field to modify as a string name |
| 764 | @param mod_field_vals Hash of values to use for modified values |
| 765 | """ |
| 766 | |
| 767 | act = None |
| 768 | |
| 769 | if field_to_mod in ['pktlen']: |
| 770 | return None |
| 771 | |
| 772 | if field_to_mod == 'dl_dst': |
Rich Lane | cd97d3d | 2013-01-07 18:50:06 -0800 | [diff] [blame] | 773 | act = of10.action.action_set_dl_dst() |
| 774 | act.dl_addr = of10.parse.parse_mac(mod_field_vals['dl_dst']) |
Dan Talayco | 4b2bee6 | 2010-07-20 14:10:05 -0700 | [diff] [blame] | 775 | elif field_to_mod == 'dl_src': |
Rich Lane | cd97d3d | 2013-01-07 18:50:06 -0800 | [diff] [blame] | 776 | act = of10.action.action_set_dl_src() |
| 777 | act.dl_addr = of10.parse.parse_mac(mod_field_vals['dl_src']) |
Dan Talayco | 4b2bee6 | 2010-07-20 14:10:05 -0700 | [diff] [blame] | 778 | elif field_to_mod == 'dl_vlan_enable': |
| 779 | if not mod_field_vals['dl_vlan_enable']: # Strip VLAN tag |
Rich Lane | cd97d3d | 2013-01-07 18:50:06 -0800 | [diff] [blame] | 780 | act = of10.action.action_strip_vlan() |
Dan Talayco | 4b2bee6 | 2010-07-20 14:10:05 -0700 | [diff] [blame] | 781 | # Add VLAN tag is handled by dl_vlan field |
| 782 | # Will return None in this case |
| 783 | elif field_to_mod == 'dl_vlan': |
Rich Lane | cd97d3d | 2013-01-07 18:50:06 -0800 | [diff] [blame] | 784 | act = of10.action.action_set_vlan_vid() |
Dan Talayco | 4b2bee6 | 2010-07-20 14:10:05 -0700 | [diff] [blame] | 785 | act.vlan_vid = mod_field_vals['dl_vlan'] |
| 786 | elif field_to_mod == 'dl_vlan_pcp': |
Rich Lane | cd97d3d | 2013-01-07 18:50:06 -0800 | [diff] [blame] | 787 | act = of10.action.action_set_vlan_pcp() |
Dan Talayco | 4b2bee6 | 2010-07-20 14:10:05 -0700 | [diff] [blame] | 788 | act.vlan_pcp = mod_field_vals['dl_vlan_pcp'] |
| 789 | elif field_to_mod == 'ip_src': |
Rich Lane | cd97d3d | 2013-01-07 18:50:06 -0800 | [diff] [blame] | 790 | act = of10.action.action_set_nw_src() |
| 791 | act.nw_addr = of10.parse.parse_ip(mod_field_vals['ip_src']) |
Dan Talayco | 4b2bee6 | 2010-07-20 14:10:05 -0700 | [diff] [blame] | 792 | elif field_to_mod == 'ip_dst': |
Rich Lane | cd97d3d | 2013-01-07 18:50:06 -0800 | [diff] [blame] | 793 | act = of10.action.action_set_nw_dst() |
| 794 | act.nw_addr = of10.parse.parse_ip(mod_field_vals['ip_dst']) |
Dan Talayco | 4b2bee6 | 2010-07-20 14:10:05 -0700 | [diff] [blame] | 795 | elif field_to_mod == 'ip_tos': |
Rich Lane | cd97d3d | 2013-01-07 18:50:06 -0800 | [diff] [blame] | 796 | act = of10.action.action_set_nw_tos() |
Dan Talayco | 4b2bee6 | 2010-07-20 14:10:05 -0700 | [diff] [blame] | 797 | act.nw_tos = mod_field_vals['ip_tos'] |
| 798 | elif field_to_mod == 'tcp_sport': |
Rich Lane | cd97d3d | 2013-01-07 18:50:06 -0800 | [diff] [blame] | 799 | act = of10.action.action_set_tp_src() |
Dan Talayco | 4b2bee6 | 2010-07-20 14:10:05 -0700 | [diff] [blame] | 800 | act.tp_port = mod_field_vals['tcp_sport'] |
| 801 | elif field_to_mod == 'tcp_dport': |
Rich Lane | cd97d3d | 2013-01-07 18:50:06 -0800 | [diff] [blame] | 802 | act = of10.action.action_set_tp_dst() |
Dan Talayco | 4b2bee6 | 2010-07-20 14:10:05 -0700 | [diff] [blame] | 803 | act.tp_port = mod_field_vals['tcp_dport'] |
Rich Lane | 110e0e3 | 2012-10-26 16:21:46 -0700 | [diff] [blame] | 804 | elif field_to_mod == 'udp_sport': |
Rich Lane | cd97d3d | 2013-01-07 18:50:06 -0800 | [diff] [blame] | 805 | act = of10.action.action_set_tp_src() |
Rich Lane | 110e0e3 | 2012-10-26 16:21:46 -0700 | [diff] [blame] | 806 | act.tp_port = mod_field_vals['udp_sport'] |
| 807 | elif field_to_mod == 'udp_dport': |
Rich Lane | cd97d3d | 2013-01-07 18:50:06 -0800 | [diff] [blame] | 808 | act = of10.action.action_set_tp_dst() |
Rich Lane | 110e0e3 | 2012-10-26 16:21:46 -0700 | [diff] [blame] | 809 | act.tp_port = mod_field_vals['udp_dport'] |
Dan Talayco | 4b2bee6 | 2010-07-20 14:10:05 -0700 | [diff] [blame] | 810 | else: |
| 811 | parent.assertTrue(0, "Unknown field to modify: " + str(field_to_mod)) |
| 812 | |
| 813 | return act |
| 814 | |
| 815 | def pkt_action_setup(parent, start_field_vals={}, mod_field_vals={}, |
Rich Lane | 110e0e3 | 2012-10-26 16:21:46 -0700 | [diff] [blame] | 816 | mod_fields=[], tp="tcp", check_test_params=False): |
Dan Talayco | 4b2bee6 | 2010-07-20 14:10:05 -0700 | [diff] [blame] | 817 | """ |
| 818 | Set up the ingress and expected packet and action list for a test |
| 819 | |
Rich Lane | 2014f9b | 2012-10-05 15:29:40 -0700 | [diff] [blame] | 820 | @param parent Must implement assertTrue |
Dan Talayco | 4b2bee6 | 2010-07-20 14:10:05 -0700 | [diff] [blame] | 821 | @param start_field_values Field values to use for ingress packet (optional) |
| 822 | @param mod_field_values Field values to use for modified packet (optional) |
| 823 | @param mod_fields The list of fields to be modified by the switch in the test. |
| 824 | @params check_test_params If True, will check the parameters vid, add_vlan |
| 825 | and strip_vlan from the command line. |
| 826 | |
| 827 | Returns a triple: pkt-to-send, expected-pkt, action-list |
| 828 | """ |
| 829 | |
| 830 | new_actions = [] |
| 831 | |
Dan Talayco | 4b2bee6 | 2010-07-20 14:10:05 -0700 | [diff] [blame] | 832 | base_pkt_params = {} |
| 833 | base_pkt_params['pktlen'] = 100 |
| 834 | base_pkt_params['dl_dst'] = '00:DE:F0:12:34:56' |
| 835 | base_pkt_params['dl_src'] = '00:23:45:67:89:AB' |
| 836 | base_pkt_params['dl_vlan_enable'] = False |
| 837 | base_pkt_params['dl_vlan'] = 2 |
| 838 | base_pkt_params['dl_vlan_pcp'] = 0 |
| 839 | base_pkt_params['ip_src'] = '192.168.0.1' |
| 840 | base_pkt_params['ip_dst'] = '192.168.0.2' |
| 841 | base_pkt_params['ip_tos'] = 0 |
Rich Lane | 110e0e3 | 2012-10-26 16:21:46 -0700 | [diff] [blame] | 842 | if tp == "tcp": |
| 843 | base_pkt_params['tcp_sport'] = 1234 |
| 844 | base_pkt_params['tcp_dport'] = 80 |
| 845 | elif tp == "udp": |
| 846 | base_pkt_params['udp_sport'] = 1234 |
| 847 | base_pkt_params['udp_dport'] = 80 |
Dan Talayco | 4b2bee6 | 2010-07-20 14:10:05 -0700 | [diff] [blame] | 848 | for keyname in start_field_vals.keys(): |
| 849 | base_pkt_params[keyname] = start_field_vals[keyname] |
| 850 | |
| 851 | mod_pkt_params = {} |
| 852 | mod_pkt_params['pktlen'] = 100 |
| 853 | mod_pkt_params['dl_dst'] = '00:21:0F:ED:CB:A9' |
| 854 | mod_pkt_params['dl_src'] = '00:ED:CB:A9:87:65' |
| 855 | mod_pkt_params['dl_vlan_enable'] = False |
| 856 | mod_pkt_params['dl_vlan'] = 3 |
| 857 | mod_pkt_params['dl_vlan_pcp'] = 7 |
| 858 | mod_pkt_params['ip_src'] = '10.20.30.40' |
| 859 | mod_pkt_params['ip_dst'] = '50.60.70.80' |
| 860 | mod_pkt_params['ip_tos'] = 0xf0 |
Rich Lane | 110e0e3 | 2012-10-26 16:21:46 -0700 | [diff] [blame] | 861 | if tp == "tcp": |
| 862 | mod_pkt_params['tcp_sport'] = 4321 |
| 863 | mod_pkt_params['tcp_dport'] = 8765 |
| 864 | elif tp == "udp": |
| 865 | mod_pkt_params['udp_sport'] = 4321 |
| 866 | mod_pkt_params['udp_dport'] = 8765 |
Dan Talayco | 4b2bee6 | 2010-07-20 14:10:05 -0700 | [diff] [blame] | 867 | for keyname in mod_field_vals.keys(): |
| 868 | mod_pkt_params[keyname] = mod_field_vals[keyname] |
| 869 | |
| 870 | # Check for test param modifications |
| 871 | strip = False |
| 872 | if check_test_params: |
Rich Lane | 2014f9b | 2012-10-05 15:29:40 -0700 | [diff] [blame] | 873 | add_vlan = test_param_get('add_vlan') |
| 874 | strip_vlan = test_param_get('strip_vlan') |
| 875 | vid = test_param_get('vid') |
Dan Talayco | 4b2bee6 | 2010-07-20 14:10:05 -0700 | [diff] [blame] | 876 | |
| 877 | if add_vlan and strip_vlan: |
| 878 | parent.assertTrue(0, "Add and strip VLAN both specified") |
| 879 | |
| 880 | if vid: |
| 881 | base_pkt_params['dl_vlan_enable'] = True |
| 882 | base_pkt_params['dl_vlan'] = vid |
| 883 | if 'dl_vlan' in mod_fields: |
| 884 | mod_pkt_params['dl_vlan'] = vid + 1 |
| 885 | |
| 886 | if add_vlan: |
| 887 | base_pkt_params['dl_vlan_enable'] = False |
| 888 | mod_pkt_params['dl_vlan_enable'] = True |
| 889 | mod_pkt_params['pktlen'] = base_pkt_params['pktlen'] + 4 |
| 890 | mod_fields.append('pktlen') |
| 891 | mod_fields.append('dl_vlan_enable') |
| 892 | if 'dl_vlan' not in mod_fields: |
| 893 | mod_fields.append('dl_vlan') |
| 894 | elif strip_vlan: |
| 895 | base_pkt_params['dl_vlan_enable'] = True |
| 896 | mod_pkt_params['dl_vlan_enable'] = False |
| 897 | mod_pkt_params['pktlen'] = base_pkt_params['pktlen'] - 4 |
| 898 | mod_fields.append('dl_vlan_enable') |
| 899 | mod_fields.append('pktlen') |
| 900 | |
Rich Lane | 110e0e3 | 2012-10-26 16:21:46 -0700 | [diff] [blame] | 901 | if tp == "tcp": |
| 902 | packet_builder = simple_tcp_packet |
| 903 | elif tp == "udp": |
| 904 | packet_builder = simple_udp_packet |
| 905 | else: |
| 906 | raise NotImplementedError("unknown transport protocol %s" % tp) |
| 907 | |
Dan Talayco | 4b2bee6 | 2010-07-20 14:10:05 -0700 | [diff] [blame] | 908 | # Build the ingress packet |
Rich Lane | 110e0e3 | 2012-10-26 16:21:46 -0700 | [diff] [blame] | 909 | ingress_pkt = packet_builder(**base_pkt_params) |
Dan Talayco | 4b2bee6 | 2010-07-20 14:10:05 -0700 | [diff] [blame] | 910 | |
| 911 | # Build the expected packet, modifying the indicated fields |
| 912 | for item in mod_fields: |
| 913 | base_pkt_params[item] = mod_pkt_params[item] |
| 914 | act = action_generate(parent, item, mod_pkt_params) |
| 915 | if act: |
| 916 | new_actions.append(act) |
| 917 | |
Rich Lane | 110e0e3 | 2012-10-26 16:21:46 -0700 | [diff] [blame] | 918 | expected_pkt = packet_builder(**base_pkt_params) |
Dan Talayco | 4b2bee6 | 2010-07-20 14:10:05 -0700 | [diff] [blame] | 919 | |
| 920 | return (ingress_pkt, expected_pkt, new_actions) |
Dan Talayco | 677c0b7 | 2011-08-23 22:53:38 -0700 | [diff] [blame] | 921 | |
| 922 | # Generate a simple "drop" flow mod |
| 923 | # If in_band is true, then only drop from first test port |
| 924 | def flow_mod_gen(port_map, in_band): |
Rich Lane | cd97d3d | 2013-01-07 18:50:06 -0800 | [diff] [blame] | 925 | request = of10.message.flow_mod() |
| 926 | request.match.wildcards = of10.cstruct.OFPFW_ALL |
Dan Talayco | 677c0b7 | 2011-08-23 22:53:38 -0700 | [diff] [blame] | 927 | if in_band: |
Rich Lane | cd97d3d | 2013-01-07 18:50:06 -0800 | [diff] [blame] | 928 | request.match.wildcards = of10.cstruct.OFPFW_ALL - of10.cstruct.OFPFW_IN_PORT |
Dan Talayco | 677c0b7 | 2011-08-23 22:53:38 -0700 | [diff] [blame] | 929 | for of_port, ifname in port_map.items(): # Grab first port |
| 930 | break |
| 931 | request.match.in_port = of_port |
| 932 | request.buffer_id = 0xffffffff |
| 933 | return request |
Dan Talayco | ba3745c | 2010-07-21 21:51:08 -0700 | [diff] [blame] | 934 | |
| 935 | def skip_message_emit(parent, s): |
| 936 | """ |
| 937 | Print out a 'skipped' message to stderr |
| 938 | |
| 939 | @param s The string to print out to the log file |
Dan Talayco | ba3745c | 2010-07-21 21:51:08 -0700 | [diff] [blame] | 940 | """ |
| 941 | global skipped_test_count |
| 942 | |
| 943 | skipped_test_count += 1 |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame] | 944 | logging.info("Skipping: " + s) |
Rich Lane | cd97d3d | 2013-01-07 18:50:06 -0800 | [diff] [blame] | 945 | if oftest.config["dbg_level"] < logging.WARNING: |
Dan Talayco | ba3745c | 2010-07-21 21:51:08 -0700 | [diff] [blame] | 946 | sys.stderr.write("(skipped) ") |
| 947 | else: |
| 948 | sys.stderr.write("(S)") |
Dan Talayco | 677c0b7 | 2011-08-23 22:53:38 -0700 | [diff] [blame] | 949 | |
Dan Talayco | 8a64e33 | 2012-03-28 14:53:20 -0700 | [diff] [blame] | 950 | |
| 951 | def all_stats_get(parent): |
| 952 | """ |
| 953 | Get the aggregate stats for all flows in the table |
| 954 | @param parent Test instance with controller connection and assert |
| 955 | @returns dict with keys flows, packets, bytes, active (flows), |
| 956 | lookups, matched |
| 957 | """ |
Rich Lane | cd97d3d | 2013-01-07 18:50:06 -0800 | [diff] [blame] | 958 | stat_req = of10.message.aggregate_stats_request() |
| 959 | stat_req.match = of10.cstruct.ofp_match() |
| 960 | stat_req.match.wildcards = of10.cstruct.OFPFW_ALL |
Dan Talayco | 8a64e33 | 2012-03-28 14:53:20 -0700 | [diff] [blame] | 961 | stat_req.table_id = 0xff |
Rich Lane | cd97d3d | 2013-01-07 18:50:06 -0800 | [diff] [blame] | 962 | stat_req.out_port = of10.cstruct.OFPP_NONE |
Dan Talayco | 8a64e33 | 2012-03-28 14:53:20 -0700 | [diff] [blame] | 963 | |
| 964 | rv = {} |
| 965 | |
Rich Lane | c8aaa3e | 2012-07-26 19:28:02 -0700 | [diff] [blame] | 966 | (reply, pkt) = parent.controller.transact(stat_req) |
Dan Talayco | 8a64e33 | 2012-03-28 14:53:20 -0700 | [diff] [blame] | 967 | parent.assertTrue(len(reply.stats) == 1, "Did not receive flow stats reply") |
| 968 | |
| 969 | for obj in reply.stats: |
| 970 | (rv["flows"], rv["packets"], rv["bytes"]) = (obj.flow_count, |
| 971 | obj.packet_count, obj.byte_count) |
| 972 | break |
| 973 | |
Rich Lane | cd97d3d | 2013-01-07 18:50:06 -0800 | [diff] [blame] | 974 | request = of10.message.table_stats_request() |
Rich Lane | c8aaa3e | 2012-07-26 19:28:02 -0700 | [diff] [blame] | 975 | (reply , pkt) = parent.controller.transact(request) |
Dan Talayco | 8a64e33 | 2012-03-28 14:53:20 -0700 | [diff] [blame] | 976 | |
| 977 | |
| 978 | (rv["active"], rv["lookups"], rv["matched"]) = (0,0,0) |
| 979 | for obj in reply.stats: |
| 980 | rv["active"] += obj.active_count |
| 981 | rv["lookups"] += obj.lookup_count |
| 982 | rv["matched"] += obj.matched_count |
| 983 | |
| 984 | return rv |
Dan Talayco | 2baf8b5 | 2012-03-30 09:55:42 -0700 | [diff] [blame] | 985 | |
| 986 | FILTER=''.join([(len(repr(chr(x)))==3) and chr(x) or '.' |
| 987 | for x in range(256)]) |
| 988 | |
| 989 | def hex_dump_buffer(src, length=16): |
| 990 | """ |
| 991 | Convert src to a hex dump string and return the string |
| 992 | @param src The source buffer |
| 993 | @param length The number of bytes shown in each line |
| 994 | @returns A string showing the hex dump |
| 995 | """ |
Dan Talayco | c516fa0 | 2012-04-12 22:28:43 -0700 | [diff] [blame] | 996 | result = ["\n"] |
Dan Talayco | 2baf8b5 | 2012-03-30 09:55:42 -0700 | [diff] [blame] | 997 | for i in xrange(0, len(src), length): |
| 998 | chars = src[i:i+length] |
| 999 | hex = ' '.join(["%02x" % ord(x) for x in chars]) |
| 1000 | printable = ''.join(["%s" % ((ord(x) <= 127 and |
| 1001 | FILTER[ord(x)]) or '.') for x in chars]) |
| 1002 | result.append("%04x %-*s %s\n" % (i, length*3, hex, printable)) |
| 1003 | return ''.join(result) |
| 1004 | |
| 1005 | def format_packet(pkt): |
| 1006 | return "Packet length %d \n%s" % (len(str(pkt)), |
| 1007 | hex_dump_buffer(str(pkt))) |
Rich Lane | 5d7e89a | 2012-10-26 16:43:13 -0700 | [diff] [blame] | 1008 | |
| 1009 | def inspect_packet(pkt): |
| 1010 | """ |
| 1011 | Wrapper around scapy's show() method. |
| 1012 | @returns A string showing the dissected packet. |
| 1013 | """ |
| 1014 | from cStringIO import StringIO |
| 1015 | out = None |
| 1016 | backup = sys.stdout |
| 1017 | try: |
| 1018 | sys.stdout = StringIO() |
| 1019 | pkt.show2() |
| 1020 | out = sys.stdout.getvalue() |
| 1021 | sys.stdout.close() |
| 1022 | finally: |
| 1023 | sys.stdout = backup |
| 1024 | return out |
Rich Lane | 0a4f637 | 2013-01-02 14:40:22 -0800 | [diff] [blame] | 1025 | |
| 1026 | def nonstandard(cls): |
| 1027 | """ |
Rich Lane | cc45b8e | 2013-01-02 15:55:02 -0800 | [diff] [blame] | 1028 | Testcase decorator that marks the test as being non-standard. |
| 1029 | These tests are not automatically added to the "standard" group. |
Rich Lane | 0a4f637 | 2013-01-02 14:40:22 -0800 | [diff] [blame] | 1030 | """ |
Rich Lane | cc45b8e | 2013-01-02 15:55:02 -0800 | [diff] [blame] | 1031 | cls._nonstandard = True |
Rich Lane | 0a4f637 | 2013-01-02 14:40:22 -0800 | [diff] [blame] | 1032 | return cls |
| 1033 | |
| 1034 | def disabled(cls): |
| 1035 | """ |
Rich Lane | cc45b8e | 2013-01-02 15:55:02 -0800 | [diff] [blame] | 1036 | Testcase decorator that marks the test as being disabled. |
| 1037 | These tests are not automatically added to the "standard" group or |
| 1038 | their module's group. |
Rich Lane | 0a4f637 | 2013-01-02 14:40:22 -0800 | [diff] [blame] | 1039 | """ |
Rich Lane | cc45b8e | 2013-01-02 15:55:02 -0800 | [diff] [blame] | 1040 | cls._disabled = True |
Rich Lane | 0a4f637 | 2013-01-02 14:40:22 -0800 | [diff] [blame] | 1041 | return cls |
Rich Lane | 97e9965 | 2013-01-02 17:23:20 -0800 | [diff] [blame] | 1042 | |
| 1043 | def group(name): |
| 1044 | """ |
| 1045 | Testcase decorator that adds the test to a group. |
| 1046 | """ |
| 1047 | def fn(cls): |
| 1048 | if not hasattr(cls, "_groups"): |
| 1049 | cls._groups = [] |
| 1050 | cls._groups.append(name) |
| 1051 | return cls |
| 1052 | return fn |