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