Dan Talayco | c901f4d | 2010-03-07 21:55:45 -0800 | [diff] [blame] | 1 | |
Dan Talayco | d2ca103 | 2010-03-10 14:40:26 -0800 | [diff] [blame] | 2 | import sys |
Dan Talayco | 92c9912 | 2010-06-03 13:53:18 -0700 | [diff] [blame] | 3 | import copy |
Dan Talayco | d2ca103 | 2010-03-10 14:40:26 -0800 | [diff] [blame] | 4 | |
| 5 | try: |
| 6 | import scapy.all as scapy |
| 7 | except: |
| 8 | try: |
| 9 | import scapy as scapy |
| 10 | except: |
| 11 | sys.exit("Need to install scapy for packet parsing") |
Dan Talayco | 41eae8b | 2010-03-10 13:57:06 -0800 | [diff] [blame] | 12 | |
Rich Lane | 477f481 | 2012-10-04 22:49:00 -0700 | [diff] [blame] | 13 | from oftest import config |
Dan Talayco | c901f4d | 2010-03-07 21:55:45 -0800 | [diff] [blame] | 14 | import oftest.controller as controller |
| 15 | import oftest.cstruct as ofp |
| 16 | import oftest.message as message |
| 17 | import oftest.dataplane as dataplane |
| 18 | import oftest.action as action |
Dan Talayco | 41eae8b | 2010-03-10 13:57:06 -0800 | [diff] [blame] | 19 | import oftest.parse as parse |
Dan Talayco | c901f4d | 2010-03-07 21:55:45 -0800 | [diff] [blame] | 20 | import logging |
Dan Talayco | 4b2bee6 | 2010-07-20 14:10:05 -0700 | [diff] [blame] | 21 | import types |
Dan Talayco | 73f8401 | 2012-10-02 09:23:18 -0700 | [diff] [blame] | 22 | import time |
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 | |
Dan Talayco | 551befa | 2010-07-15 17:05:32 -0700 | [diff] [blame] | 27 | # Some useful defines |
| 28 | IP_ETHERTYPE = 0x800 |
| 29 | TCP_PROTOCOL = 0x6 |
| 30 | UDP_PROTOCOL = 0x11 |
| 31 | |
Jeffrey Townsend | 5cb7ed3 | 2012-08-17 18:11:01 +0000 | [diff] [blame] | 32 | MINSIZE = 0 |
| 33 | |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame] | 34 | def delete_all_flows(ctrl): |
Dan Talayco | 41eae8b | 2010-03-10 13:57:06 -0800 | [diff] [blame] | 35 | """ |
| 36 | Delete all flows on the switch |
| 37 | @param ctrl The controller object for the test |
Dan Talayco | 41eae8b | 2010-03-10 13:57:06 -0800 | [diff] [blame] | 38 | """ |
| 39 | |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame] | 40 | logging.info("Deleting all flows") |
Dan Talayco | c901f4d | 2010-03-07 21:55:45 -0800 | [diff] [blame] | 41 | msg = message.flow_mod() |
| 42 | msg.match.wildcards = ofp.OFPFW_ALL |
Dan Talayco | 41eae8b | 2010-03-10 13:57:06 -0800 | [diff] [blame] | 43 | msg.out_port = ofp.OFPP_NONE |
Dan Talayco | c901f4d | 2010-03-07 21:55:45 -0800 | [diff] [blame] | 44 | msg.command = ofp.OFPFC_DELETE |
| 45 | msg.buffer_id = 0xffffffff |
Dan Talayco | 41eae8b | 2010-03-10 13:57:06 -0800 | [diff] [blame] | 46 | return ctrl.message_send(msg) |
| 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': |
| 51 | return (ofp.OFPFW_NW_SRC_ALL | ofp.OFPFW_NW_DST_ALL | ofp.OFPFW_NW_TOS |
| 52 | | ofp.OFPFW_NW_PROTO | ofp.OFPFW_TP_SRC | ofp.OFPFW_TP_DST) |
| 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 | """ |
Dan Talayco | 41eae8b | 2010-03-10 13:57:06 -0800 | [diff] [blame] | 297 | b = message.barrier_request() |
Shudong Zhou | b7f1246 | 2012-11-20 13:01:12 -0800 | [diff] [blame] | 298 | (resp, pkt) = ctrl.transact(b, timeout=timeout) |
Dan Talayco | f6b9483 | 2012-04-12 21:50:57 -0700 | [diff] [blame] | 299 | # We'll trust the transaction processing in the controller that xid matched |
Dan Talayco | 0fc08bd | 2012-04-09 16:56:18 -0700 | [diff] [blame] | 300 | if not resp: |
| 301 | return -1 |
| 302 | return 0 |
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 | """ |
| 314 | request = 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)) |
Dan Talayco | 92c9912 | 2010-06-03 13:53:18 -0700 | [diff] [blame] | 336 | request = 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()) |
Dan Talayco | 92c9912 | 2010-06-03 13:53:18 -0700 | [diff] [blame] | 341 | for idx in range(len(reply.ports)): |
| 342 | if reply.ports[idx].port_no == port_no: |
| 343 | break |
| 344 | if idx >= len(reply.ports): |
| 345 | return -1 |
| 346 | mod = message.port_mod() |
| 347 | mod.port_no = port_no |
| 348 | mod.hw_addr = reply.ports[idx].hw_addr |
| 349 | mod.config = config |
| 350 | mod.mask = mask |
| 351 | mod.advertise = reply.ports[idx].advertised |
| 352 | rv = controller.message_send(mod) |
| 353 | return rv |
| 354 | |
Rich Lane | 2014f9b | 2012-10-05 15:29:40 -0700 | [diff] [blame] | 355 | def receive_pkt_check(dp, pkt, yes_ports, no_ports, assert_if): |
Dan Talayco | 92c9912 | 2010-06-03 13:53:18 -0700 | [diff] [blame] | 356 | """ |
| 357 | Check for proper receive packets across all ports |
Ken Chiang | 1bf0160 | 2012-04-04 10:48:23 -0700 | [diff] [blame] | 358 | @param dp The dataplane object |
Dan Talayco | 92c9912 | 2010-06-03 13:53:18 -0700 | [diff] [blame] | 359 | @param pkt Expected packet; may be None if yes_ports is empty |
| 360 | @param yes_ports Set or list of ports that should recieve packet |
| 361 | @param no_ports Set or list of ports that should not receive packet |
| 362 | @param assert_if Object that implements assertXXX |
| 363 | """ |
Rich Lane | 9176567 | 2012-12-06 16:33:04 -0800 | [diff] [blame] | 364 | |
| 365 | # Wait this long for packets that we don't expect to receive. |
| 366 | # 100ms is (rarely) too short for positive tests on slow |
| 367 | # switches but is definitely not too short for a negative test. |
| 368 | negative_timeout = 0.1 |
| 369 | |
Dan Talayco | cf26b7a | 2011-08-05 10:15:35 -0700 | [diff] [blame] | 370 | exp_pkt_arg = None |
Rich Lane | 2014f9b | 2012-10-05 15:29:40 -0700 | [diff] [blame] | 371 | if config["relax"]: |
Dan Talayco | cf26b7a | 2011-08-05 10:15:35 -0700 | [diff] [blame] | 372 | exp_pkt_arg = pkt |
| 373 | |
Dan Talayco | 92c9912 | 2010-06-03 13:53:18 -0700 | [diff] [blame] | 374 | for ofport in yes_ports: |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame] | 375 | logging.debug("Checking for pkt on port " + str(ofport)) |
Ken Chiang | 1bf0160 | 2012-04-04 10:48:23 -0700 | [diff] [blame] | 376 | (rcv_port, rcv_pkt, pkt_time) = dp.poll( |
Rich Lane | c8aaa3e | 2012-07-26 19:28:02 -0700 | [diff] [blame] | 377 | port_number=ofport, exp_pkt=exp_pkt_arg) |
Dan Talayco | 92c9912 | 2010-06-03 13:53:18 -0700 | [diff] [blame] | 378 | assert_if.assertTrue(rcv_pkt is not None, |
| 379 | "Did not receive pkt on " + str(ofport)) |
Ken Chiang | 1bf0160 | 2012-04-04 10:48:23 -0700 | [diff] [blame] | 380 | if not dataplane.match_exp_pkt(pkt, rcv_pkt): |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame] | 381 | logging.debug("Sent %s" % format_packet(pkt)) |
| 382 | logging.debug("Resp %s" % format_packet(rcv_pkt)) |
Ken Chiang | 1bf0160 | 2012-04-04 10:48:23 -0700 | [diff] [blame] | 383 | assert_if.assertTrue(dataplane.match_exp_pkt(pkt, rcv_pkt), |
| 384 | "Response packet does not match send packet " + |
| 385 | "on port " + str(ofport)) |
Dan Talayco | 73f8401 | 2012-10-02 09:23:18 -0700 | [diff] [blame] | 386 | if len(no_ports) > 0: |
Rich Lane | 9176567 | 2012-12-06 16:33:04 -0800 | [diff] [blame] | 387 | time.sleep(negative_timeout) |
Dan Talayco | 92c9912 | 2010-06-03 13:53:18 -0700 | [diff] [blame] | 388 | for ofport in no_ports: |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame] | 389 | logging.debug("Negative check for pkt on port " + str(ofport)) |
Ken Chiang | 1bf0160 | 2012-04-04 10:48:23 -0700 | [diff] [blame] | 390 | (rcv_port, rcv_pkt, pkt_time) = dp.poll( |
Rich Lane | 9176567 | 2012-12-06 16:33:04 -0800 | [diff] [blame] | 391 | port_number=ofport, timeout=0, exp_pkt=exp_pkt_arg) |
Dan Talayco | 92c9912 | 2010-06-03 13:53:18 -0700 | [diff] [blame] | 392 | assert_if.assertTrue(rcv_pkt is None, |
| 393 | "Unexpected pkt on port " + str(ofport)) |
Dan Talayco | 551befa | 2010-07-15 17:05:32 -0700 | [diff] [blame] | 394 | |
| 395 | |
Dan Talayco | c948d0b | 2012-03-23 12:17:54 -0700 | [diff] [blame] | 396 | def receive_pkt_verify(parent, egr_ports, exp_pkt, ing_port): |
Dan Talayco | 551befa | 2010-07-15 17:05:32 -0700 | [diff] [blame] | 397 | """ |
| 398 | Receive a packet and verify it matches an expected value |
Dan Talayco | f6e76c0 | 2012-03-23 10:56:12 -0700 | [diff] [blame] | 399 | @param egr_port A single port or list of ports |
Dan Talayco | 551befa | 2010-07-15 17:05:32 -0700 | [diff] [blame] | 400 | |
| 401 | parent must implement dataplane, assertTrue and assertEqual |
| 402 | """ |
Dan Talayco | cf26b7a | 2011-08-05 10:15:35 -0700 | [diff] [blame] | 403 | exp_pkt_arg = None |
Rich Lane | 477f481 | 2012-10-04 22:49:00 -0700 | [diff] [blame] | 404 | if config["relax"]: |
Dan Talayco | cf26b7a | 2011-08-05 10:15:35 -0700 | [diff] [blame] | 405 | exp_pkt_arg = exp_pkt |
| 406 | |
Dan Talayco | f6e76c0 | 2012-03-23 10:56:12 -0700 | [diff] [blame] | 407 | if type(egr_ports) == type([]): |
| 408 | egr_port_list = egr_ports |
| 409 | else: |
| 410 | egr_port_list = [egr_ports] |
Dan Talayco | cf26b7a | 2011-08-05 10:15:35 -0700 | [diff] [blame] | 411 | |
Dan Talayco | f6e76c0 | 2012-03-23 10:56:12 -0700 | [diff] [blame] | 412 | # Expect a packet from each port on egr port list |
| 413 | for egr_port in egr_port_list: |
Dan Talayco | d8ae758 | 2012-03-23 12:24:56 -0700 | [diff] [blame] | 414 | check_port = egr_port |
Dan Talayco | c948d0b | 2012-03-23 12:17:54 -0700 | [diff] [blame] | 415 | if egr_port == ofp.OFPP_IN_PORT: |
Dan Talayco | d8ae758 | 2012-03-23 12:24:56 -0700 | [diff] [blame] | 416 | check_port = ing_port |
Dan Talayco | f6e76c0 | 2012-03-23 10:56:12 -0700 | [diff] [blame] | 417 | (rcv_port, rcv_pkt, pkt_time) = parent.dataplane.poll( |
Rich Lane | c8aaa3e | 2012-07-26 19:28:02 -0700 | [diff] [blame] | 418 | port_number=check_port, exp_pkt=exp_pkt_arg) |
Dan Talayco | 551befa | 2010-07-15 17:05:32 -0700 | [diff] [blame] | 419 | |
Dan Talayco | f6e76c0 | 2012-03-23 10:56:12 -0700 | [diff] [blame] | 420 | if rcv_pkt is None: |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame] | 421 | logging.error("ERROR: No packet received from " + |
Dan Talayco | d8ae758 | 2012-03-23 12:24:56 -0700 | [diff] [blame] | 422 | str(check_port)) |
Dan Talayco | 551befa | 2010-07-15 17:05:32 -0700 | [diff] [blame] | 423 | |
Dan Talayco | f6e76c0 | 2012-03-23 10:56:12 -0700 | [diff] [blame] | 424 | parent.assertTrue(rcv_pkt is not None, |
Dan Talayco | d8ae758 | 2012-03-23 12:24:56 -0700 | [diff] [blame] | 425 | "Did not receive packet port " + str(check_port)) |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame] | 426 | logging.debug("Packet len " + str(len(rcv_pkt)) + " in on " + |
Dan Talayco | f6e76c0 | 2012-03-23 10:56:12 -0700 | [diff] [blame] | 427 | str(rcv_port)) |
| 428 | |
| 429 | if str(exp_pkt) != str(rcv_pkt): |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame] | 430 | logging.error("ERROR: Packet match failed.") |
| 431 | logging.debug("Expected len " + str(len(exp_pkt)) + ": " |
Dan Talayco | f6e76c0 | 2012-03-23 10:56:12 -0700 | [diff] [blame] | 432 | + str(exp_pkt).encode('hex')) |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame] | 433 | logging.debug("Received len " + str(len(rcv_pkt)) + ": " |
Dan Talayco | f6e76c0 | 2012-03-23 10:56:12 -0700 | [diff] [blame] | 434 | + str(rcv_pkt).encode('hex')) |
Rich Lane | 5d7e89a | 2012-10-26 16:43:13 -0700 | [diff] [blame] | 435 | logging.debug("Expected packet: " + inspect_packet(scapy.Ether(str(exp_pkt)))) |
| 436 | logging.debug("Received packet: " + inspect_packet(scapy.Ether(str(rcv_pkt)))) |
Dan Talayco | f6e76c0 | 2012-03-23 10:56:12 -0700 | [diff] [blame] | 437 | parent.assertEqual(str(exp_pkt), str(rcv_pkt), |
Dan Talayco | d8ae758 | 2012-03-23 12:24:56 -0700 | [diff] [blame] | 438 | "Packet match error on port " + str(check_port)) |
Dan Talayco | f6e76c0 | 2012-03-23 10:56:12 -0700 | [diff] [blame] | 439 | |
Dan Talayco | 551befa | 2010-07-15 17:05:32 -0700 | [diff] [blame] | 440 | def match_verify(parent, req_match, res_match): |
| 441 | """ |
| 442 | Verify flow matches agree; if they disagree, report where |
| 443 | |
| 444 | parent must implement assertEqual |
| 445 | Use str() to ensure content is compared and not pointers |
| 446 | """ |
| 447 | |
| 448 | parent.assertEqual(req_match.wildcards, res_match.wildcards, |
| 449 | 'Match failed: wildcards: ' + hex(req_match.wildcards) + |
| 450 | " != " + hex(res_match.wildcards)) |
| 451 | parent.assertEqual(req_match.in_port, res_match.in_port, |
| 452 | 'Match failed: in_port: ' + str(req_match.in_port) + |
| 453 | " != " + str(res_match.in_port)) |
| 454 | parent.assertEqual(str(req_match.dl_src), str(res_match.dl_src), |
| 455 | 'Match failed: dl_src: ' + str(req_match.dl_src) + |
| 456 | " != " + str(res_match.dl_src)) |
| 457 | parent.assertEqual(str(req_match.dl_dst), str(res_match.dl_dst), |
| 458 | 'Match failed: dl_dst: ' + str(req_match.dl_dst) + |
| 459 | " != " + str(res_match.dl_dst)) |
| 460 | parent.assertEqual(req_match.dl_vlan, res_match.dl_vlan, |
| 461 | 'Match failed: dl_vlan: ' + str(req_match.dl_vlan) + |
| 462 | " != " + str(res_match.dl_vlan)) |
| 463 | parent.assertEqual(req_match.dl_vlan_pcp, res_match.dl_vlan_pcp, |
| 464 | 'Match failed: dl_vlan_pcp: ' + |
| 465 | str(req_match.dl_vlan_pcp) + " != " + |
| 466 | str(res_match.dl_vlan_pcp)) |
| 467 | parent.assertEqual(req_match.dl_type, res_match.dl_type, |
| 468 | 'Match failed: dl_type: ' + str(req_match.dl_type) + |
| 469 | " != " + str(res_match.dl_type)) |
| 470 | |
| 471 | if (not(req_match.wildcards & ofp.OFPFW_DL_TYPE) |
| 472 | and (req_match.dl_type == IP_ETHERTYPE)): |
| 473 | parent.assertEqual(req_match.nw_tos, res_match.nw_tos, |
| 474 | 'Match failed: nw_tos: ' + str(req_match.nw_tos) + |
| 475 | " != " + str(res_match.nw_tos)) |
| 476 | parent.assertEqual(req_match.nw_proto, res_match.nw_proto, |
| 477 | 'Match failed: nw_proto: ' + str(req_match.nw_proto) + |
| 478 | " != " + str(res_match.nw_proto)) |
| 479 | parent.assertEqual(req_match.nw_src, res_match.nw_src, |
| 480 | 'Match failed: nw_src: ' + str(req_match.nw_src) + |
| 481 | " != " + str(res_match.nw_src)) |
| 482 | parent.assertEqual(req_match.nw_dst, res_match.nw_dst, |
| 483 | 'Match failed: nw_dst: ' + str(req_match.nw_dst) + |
| 484 | " != " + str(res_match.nw_dst)) |
| 485 | |
| 486 | if (not(req_match.wildcards & ofp.OFPFW_NW_PROTO) |
| 487 | and ((req_match.nw_proto == TCP_PROTOCOL) |
| 488 | or (req_match.nw_proto == UDP_PROTOCOL))): |
| 489 | parent.assertEqual(req_match.tp_src, res_match.tp_src, |
| 490 | 'Match failed: tp_src: ' + |
| 491 | str(req_match.tp_src) + |
| 492 | " != " + str(res_match.tp_src)) |
| 493 | parent.assertEqual(req_match.tp_dst, res_match.tp_dst, |
| 494 | 'Match failed: tp_dst: ' + |
| 495 | str(req_match.tp_dst) + |
| 496 | " != " + str(res_match.tp_dst)) |
| 497 | |
Ed Swierk | 99a74de | 2012-08-22 06:40:54 -0700 | [diff] [blame] | 498 | def packet_to_flow_match(parent, packet): |
| 499 | match = parse.packet_to_flow_match(packet) |
| 500 | match.wildcards |= required_wildcards(parent) |
| 501 | return match |
| 502 | |
| 503 | 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] | 504 | egr_ports=None, egr_queue=None, check_expire=False, in_band=False): |
Dan Talayco | 551befa | 2010-07-15 17:05:32 -0700 | [diff] [blame] | 505 | """ |
| 506 | Create a flow message |
| 507 | |
| 508 | Match on packet with given wildcards. |
| 509 | See flow_match_test for other parameter descriptoins |
| 510 | @param egr_queue if not None, make the output an enqueue action |
Dan Talayco | 677c0b7 | 2011-08-23 22:53:38 -0700 | [diff] [blame] | 511 | @param in_band if True, do not wildcard ingress port |
Dan Talayco | f6e76c0 | 2012-03-23 10:56:12 -0700 | [diff] [blame] | 512 | @param egr_ports None (drop), single port or list of ports |
Dan Talayco | 551befa | 2010-07-15 17:05:32 -0700 | [diff] [blame] | 513 | """ |
| 514 | match = parse.packet_to_flow_match(pkt) |
| 515 | parent.assertTrue(match is not None, "Flow match from pkt failed") |
Ed Swierk | 99a74de | 2012-08-22 06:40:54 -0700 | [diff] [blame] | 516 | if wildcards is None: |
| 517 | wildcards = required_wildcards(parent) |
Dan Talayco | 677c0b7 | 2011-08-23 22:53:38 -0700 | [diff] [blame] | 518 | if in_band: |
| 519 | wildcards &= ~ofp.OFPFW_IN_PORT |
Dan Talayco | 551befa | 2010-07-15 17:05:32 -0700 | [diff] [blame] | 520 | match.wildcards = wildcards |
| 521 | match.in_port = ing_port |
| 522 | |
Dan Talayco | f6e76c0 | 2012-03-23 10:56:12 -0700 | [diff] [blame] | 523 | if type(egr_ports) == type([]): |
| 524 | egr_port_list = egr_ports |
| 525 | else: |
| 526 | egr_port_list = [egr_ports] |
| 527 | |
Dan Talayco | 551befa | 2010-07-15 17:05:32 -0700 | [diff] [blame] | 528 | request = message.flow_mod() |
| 529 | request.match = match |
| 530 | request.buffer_id = 0xffffffff |
| 531 | if check_expire: |
| 532 | request.flags |= ofp.OFPFF_SEND_FLOW_REM |
| 533 | request.hard_timeout = 1 |
| 534 | |
| 535 | if action_list is not None: |
| 536 | for act in action_list: |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame] | 537 | logging.debug("Adding action " + act.show()) |
Dan Talayco | 551befa | 2010-07-15 17:05:32 -0700 | [diff] [blame] | 538 | rv = request.actions.add(act) |
| 539 | parent.assertTrue(rv, "Could not add action" + act.show()) |
| 540 | |
| 541 | # Set up output/enqueue action if directed |
| 542 | if egr_queue is not None: |
Dan Talayco | f6e76c0 | 2012-03-23 10:56:12 -0700 | [diff] [blame] | 543 | parent.assertTrue(egr_ports is not None, "Egress port not set") |
Dan Talayco | 551befa | 2010-07-15 17:05:32 -0700 | [diff] [blame] | 544 | act = action.action_enqueue() |
Dan Talayco | f6e76c0 | 2012-03-23 10:56:12 -0700 | [diff] [blame] | 545 | for egr_port in egr_port_list: |
| 546 | act.port = egr_port |
| 547 | act.queue_id = egr_queue |
| 548 | rv = request.actions.add(act) |
| 549 | parent.assertTrue(rv, "Could not add enqueue action " + |
| 550 | str(egr_port) + " Q: " + str(egr_queue)) |
| 551 | elif egr_ports is not None: |
| 552 | for egr_port in egr_port_list: |
| 553 | act = action.action_output() |
| 554 | act.port = egr_port |
| 555 | rv = request.actions.add(act) |
| 556 | parent.assertTrue(rv, "Could not add output action " + |
| 557 | str(egr_port)) |
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") |
| 578 | rc = delete_all_flows(parent.controller) |
Dan Talayco | 551befa | 2010-07-15 17:05:32 -0700 | [diff] [blame] | 579 | parent.assertEqual(rc, 0, "Failed to delete all flows") |
Dan Talayco | 551befa | 2010-07-15 17:05:32 -0700 | [diff] [blame] | 580 | |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame] | 581 | logging.debug("Insert flow") |
Dan Talayco | 551befa | 2010-07-15 17:05:32 -0700 | [diff] [blame] | 582 | rv = parent.controller.message_send(request) |
| 583 | parent.assertTrue(rv != -1, "Error installing flow mod") |
Rich Lane | e912d03 | 2012-12-22 14:28:33 -0800 | [diff] [blame] | 584 | |
Dan Talayco | 0fc08bd | 2012-04-09 16:56:18 -0700 | [diff] [blame] | 585 | parent.assertEqual(do_barrier(parent.controller), 0, "Barrier failed") |
Dan Talayco | 551befa | 2010-07-15 17:05:32 -0700 | [diff] [blame] | 586 | |
Ed Swierk | 99a74de | 2012-08-22 06:40:54 -0700 | [diff] [blame] | 587 | 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] | 588 | dl_vlan=-1, pkt=None, exp_pkt=None, |
Rich Lane | e5779d3 | 2012-10-05 17:56:04 -0700 | [diff] [blame] | 589 | action_list=None): |
Dan Talayco | 551befa | 2010-07-15 17:05:32 -0700 | [diff] [blame] | 590 | """ |
| 591 | Flow match test on single TCP packet |
Dan Talayco | f6e76c0 | 2012-03-23 10:56:12 -0700 | [diff] [blame] | 592 | @param egr_ports A single port or list of ports |
Dan Talayco | 551befa | 2010-07-15 17:05:32 -0700 | [diff] [blame] | 593 | |
| 594 | Run test with packet through switch from ing_port to egr_port |
| 595 | See flow_match_test for parameter descriptions |
| 596 | """ |
| 597 | |
Ed Swierk | 99a74de | 2012-08-22 06:40:54 -0700 | [diff] [blame] | 598 | if wildcards is None: |
| 599 | wildcards = required_wildcards(parent) |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame] | 600 | logging.info("Pkt match test: " + str(ing_port) + " to " + |
Dan Talayco | f6e76c0 | 2012-03-23 10:56:12 -0700 | [diff] [blame] | 601 | str(egr_ports)) |
Rich Lane | e5779d3 | 2012-10-05 17:56:04 -0700 | [diff] [blame] | 602 | logging.debug(" WC: " + hex(wildcards) + " vlan: " + str(dl_vlan)) |
Dan Talayco | 551befa | 2010-07-15 17:05:32 -0700 | [diff] [blame] | 603 | if pkt is None: |
| 604 | pkt = simple_tcp_packet(dl_vlan_enable=(dl_vlan >= 0), dl_vlan=dl_vlan) |
| 605 | |
| 606 | request = flow_msg_create(parent, pkt, ing_port=ing_port, |
Dan Talayco | f6e76c0 | 2012-03-23 10:56:12 -0700 | [diff] [blame] | 607 | wildcards=wildcards, egr_ports=egr_ports, |
Dan Talayco | 551befa | 2010-07-15 17:05:32 -0700 | [diff] [blame] | 608 | action_list=action_list) |
| 609 | |
| 610 | flow_msg_install(parent, request) |
| 611 | |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame] | 612 | logging.debug("Send packet: " + str(ing_port) + " to " + |
Dan Talayco | f6e76c0 | 2012-03-23 10:56:12 -0700 | [diff] [blame] | 613 | str(egr_ports)) |
Dan Talayco | 551befa | 2010-07-15 17:05:32 -0700 | [diff] [blame] | 614 | parent.dataplane.send(ing_port, str(pkt)) |
| 615 | |
| 616 | if exp_pkt is None: |
| 617 | exp_pkt = pkt |
Dan Talayco | c948d0b | 2012-03-23 12:17:54 -0700 | [diff] [blame] | 618 | receive_pkt_verify(parent, egr_ports, exp_pkt, ing_port) |
Dan Talayco | 551befa | 2010-07-15 17:05:32 -0700 | [diff] [blame] | 619 | |
Rich Lane | 89725bb | 2012-12-03 16:23:27 -0800 | [diff] [blame] | 620 | def flow_match_test_pktout(parent, ing_port, egr_ports, |
| 621 | dl_vlan=-1, pkt=None, exp_pkt=None, |
| 622 | action_list=None): |
| 623 | """ |
| 624 | Packet-out test on single TCP packet |
| 625 | @param egr_ports A single port or list of ports |
| 626 | |
| 627 | Run test sending packet-out to egr_ports. The goal is to test the actions |
| 628 | taken on the packet, not the matching which is of course irrelevant. |
| 629 | See flow_match_test for parameter descriptions |
| 630 | """ |
| 631 | |
| 632 | if pkt is None: |
| 633 | pkt = simple_tcp_packet(dl_vlan_enable=(dl_vlan >= 0), dl_vlan=dl_vlan) |
| 634 | |
| 635 | msg = message.packet_out() |
| 636 | msg.in_port = ing_port |
| 637 | msg.data = str(pkt) |
| 638 | if action_list is not None: |
| 639 | for act in action_list: |
| 640 | assert(msg.actions.add(act)) |
| 641 | |
| 642 | # Set up output action |
| 643 | if egr_ports is not None: |
| 644 | for egr_port in egr_ports: |
| 645 | act = action.action_output() |
| 646 | act.port = egr_port |
| 647 | assert(msg.actions.add(act)) |
| 648 | |
| 649 | logging.debug(msg.show()) |
| 650 | rv = parent.controller.message_send(msg) |
| 651 | parent.assertTrue(rv == 0, "Error sending out message") |
| 652 | |
| 653 | if exp_pkt is None: |
| 654 | exp_pkt = pkt |
| 655 | receive_pkt_verify(parent, egr_ports, exp_pkt, ing_port) |
| 656 | |
Dan Talayco | f6e76c0 | 2012-03-23 10:56:12 -0700 | [diff] [blame] | 657 | def get_egr_list(parent, of_ports, how_many, exclude_list=[]): |
| 658 | """ |
| 659 | Generate a list of ports avoiding those in the exclude list |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame] | 660 | @param parent Supplies logging |
Dan Talayco | f6e76c0 | 2012-03-23 10:56:12 -0700 | [diff] [blame] | 661 | @param of_ports List of OF port numbers |
| 662 | @param how_many Number of ports to be added to the list |
| 663 | @param exclude_list List of ports not to be used |
| 664 | @returns An empty list if unable to find enough ports |
| 665 | """ |
| 666 | |
Dan Talayco | c948d0b | 2012-03-23 12:17:54 -0700 | [diff] [blame] | 667 | if how_many == 0: |
| 668 | return [] |
| 669 | |
Dan Talayco | f6e76c0 | 2012-03-23 10:56:12 -0700 | [diff] [blame] | 670 | count = 0 |
| 671 | egr_ports = [] |
| 672 | for egr_idx in range(len(of_ports)): |
| 673 | if of_ports[egr_idx] not in exclude_list: |
| 674 | egr_ports.append(of_ports[egr_idx]) |
| 675 | count += 1 |
| 676 | if count >= how_many: |
| 677 | return egr_ports |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame] | 678 | logging.debug("Could not generate enough egress ports for test") |
Dan Talayco | f6e76c0 | 2012-03-23 10:56:12 -0700 | [diff] [blame] | 679 | return [] |
| 680 | |
Ed Swierk | 99a74de | 2012-08-22 06:40:54 -0700 | [diff] [blame] | 681 | 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] | 682 | exp_pkt=None, action_list=None, |
Dan Talayco | c948d0b | 2012-03-23 12:17:54 -0700 | [diff] [blame] | 683 | max_test=0, egr_count=1, ing_port=False): |
Dan Talayco | 551befa | 2010-07-15 17:05:32 -0700 | [diff] [blame] | 684 | """ |
Rich Lane | 89725bb | 2012-12-03 16:23:27 -0800 | [diff] [blame] | 685 | 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] | 686 | |
| 687 | @param max_test If > 0 no more than this number of tests are executed. |
| 688 | @param parent Must implement controller, dataplane, assertTrue, assertEqual |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame] | 689 | and logging |
Dan Talayco | 551befa | 2010-07-15 17:05:32 -0700 | [diff] [blame] | 690 | @param pkt If not None, use this packet for ingress |
| 691 | @param wildcards For flow match entry |
Dan Talayco | 7918422 | 2010-11-01 12:24:29 -0700 | [diff] [blame] | 692 | @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] | 693 | @param exp_pkt If not None, use this as the expected output pkt; els use pkt |
| 694 | @param action_list Additional actions to add to flow mod |
Dan Talayco | cfa172f | 2012-03-23 12:03:00 -0700 | [diff] [blame] | 695 | @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] | 696 | """ |
Ed Swierk | 99a74de | 2012-08-22 06:40:54 -0700 | [diff] [blame] | 697 | if wildcards is None: |
| 698 | wildcards = required_wildcards(parent) |
Dan Talayco | 551befa | 2010-07-15 17:05:32 -0700 | [diff] [blame] | 699 | of_ports = port_map.keys() |
| 700 | of_ports.sort() |
| 701 | parent.assertTrue(len(of_ports) > 1, "Not enough ports for test") |
| 702 | test_count = 0 |
| 703 | |
Dan Talayco | cfa172f | 2012-03-23 12:03:00 -0700 | [diff] [blame] | 704 | if egr_count == -1: |
Rich Lane | 2014f9b | 2012-10-05 15:29:40 -0700 | [diff] [blame] | 705 | egr_count = test_param_get('egr_count', default=2) |
Dan Talayco | cfa172f | 2012-03-23 12:03:00 -0700 | [diff] [blame] | 706 | |
Dan Talayco | 551befa | 2010-07-15 17:05:32 -0700 | [diff] [blame] | 707 | for ing_idx in range(len(of_ports)): |
| 708 | ingress_port = of_ports[ing_idx] |
Dan Talayco | f6e76c0 | 2012-03-23 10:56:12 -0700 | [diff] [blame] | 709 | egr_ports = get_egr_list(parent, of_ports, egr_count, |
| 710 | exclude_list=[ingress_port]) |
Dan Talayco | c948d0b | 2012-03-23 12:17:54 -0700 | [diff] [blame] | 711 | if ing_port: |
| 712 | egr_ports.append(ofp.OFPP_IN_PORT) |
Dan Talayco | f6e76c0 | 2012-03-23 10:56:12 -0700 | [diff] [blame] | 713 | if len(egr_ports) == 0: |
| 714 | parent.assertTrue(0, "Failed to generate egress port list") |
| 715 | |
| 716 | flow_match_test_port_pair(parent, ingress_port, egr_ports, |
| 717 | wildcards=wildcards, dl_vlan=dl_vlan, |
| 718 | pkt=pkt, exp_pkt=exp_pkt, |
Rich Lane | e5779d3 | 2012-10-05 17:56:04 -0700 | [diff] [blame] | 719 | action_list=action_list) |
Dan Talayco | f6e76c0 | 2012-03-23 10:56:12 -0700 | [diff] [blame] | 720 | test_count += 1 |
| 721 | if (max_test > 0) and (test_count > max_test): |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame] | 722 | logging.info("Ran " + str(test_count) + " tests; exiting") |
Rich Lane | 89725bb | 2012-12-03 16:23:27 -0800 | [diff] [blame] | 723 | break |
| 724 | |
| 725 | |
| 726 | ingress_port = of_ports[0] |
| 727 | egr_ports = get_egr_list(parent, of_ports, egr_count, |
| 728 | exclude_list=[ingress_port]) |
| 729 | if ing_port: |
| 730 | egr_ports.append(ofp.OFPP_IN_PORT) |
| 731 | flow_match_test_pktout(parent, ingress_port, egr_ports, |
| 732 | dl_vlan=dl_vlan, |
| 733 | pkt=pkt, exp_pkt=exp_pkt, |
| 734 | action_list=action_list) |
Dan Talayco | 551befa | 2010-07-15 17:05:32 -0700 | [diff] [blame] | 735 | |
Rich Lane | 2014f9b | 2012-10-05 15:29:40 -0700 | [diff] [blame] | 736 | def test_param_get(key, default=None): |
Dan Talayco | 4b2bee6 | 2010-07-20 14:10:05 -0700 | [diff] [blame] | 737 | """ |
| 738 | Return value passed via test-params if present |
| 739 | |
Dan Talayco | 4b2bee6 | 2010-07-20 14:10:05 -0700 | [diff] [blame] | 740 | @param key The lookup key |
| 741 | @param default Default value to use if not found |
| 742 | |
| 743 | If the pair 'key=val' appeared in the string passed to --test-params |
| 744 | on the command line, return val (as interpreted by exec). Otherwise |
| 745 | return default value. |
Dan Talayco | f6e76c0 | 2012-03-23 10:56:12 -0700 | [diff] [blame] | 746 | |
| 747 | WARNING: TEST PARAMETERS MUST BE PYTHON IDENTIFIERS; |
| 748 | eg egr_count, not egr-count. |
Dan Talayco | 4b2bee6 | 2010-07-20 14:10:05 -0700 | [diff] [blame] | 749 | """ |
| 750 | try: |
| 751 | exec config["test_params"] |
| 752 | except: |
| 753 | return default |
| 754 | |
| 755 | s = "val = " + str(key) |
| 756 | try: |
| 757 | exec s |
| 758 | return val |
| 759 | except: |
| 760 | return default |
| 761 | |
| 762 | def action_generate(parent, field_to_mod, mod_field_vals): |
| 763 | """ |
| 764 | Create an action to modify the field indicated in field_to_mod |
| 765 | |
| 766 | @param parent Must implement, assertTrue |
| 767 | @param field_to_mod The field to modify as a string name |
| 768 | @param mod_field_vals Hash of values to use for modified values |
| 769 | """ |
| 770 | |
| 771 | act = None |
| 772 | |
| 773 | if field_to_mod in ['pktlen']: |
| 774 | return None |
| 775 | |
| 776 | if field_to_mod == 'dl_dst': |
| 777 | act = action.action_set_dl_dst() |
| 778 | act.dl_addr = parse.parse_mac(mod_field_vals['dl_dst']) |
| 779 | elif field_to_mod == 'dl_src': |
| 780 | act = action.action_set_dl_src() |
| 781 | act.dl_addr = parse.parse_mac(mod_field_vals['dl_src']) |
| 782 | elif field_to_mod == 'dl_vlan_enable': |
| 783 | if not mod_field_vals['dl_vlan_enable']: # Strip VLAN tag |
| 784 | act = action.action_strip_vlan() |
| 785 | # Add VLAN tag is handled by dl_vlan field |
| 786 | # Will return None in this case |
| 787 | elif field_to_mod == 'dl_vlan': |
| 788 | act = action.action_set_vlan_vid() |
| 789 | act.vlan_vid = mod_field_vals['dl_vlan'] |
| 790 | elif field_to_mod == 'dl_vlan_pcp': |
| 791 | act = action.action_set_vlan_pcp() |
| 792 | act.vlan_pcp = mod_field_vals['dl_vlan_pcp'] |
| 793 | elif field_to_mod == 'ip_src': |
| 794 | act = action.action_set_nw_src() |
| 795 | act.nw_addr = parse.parse_ip(mod_field_vals['ip_src']) |
| 796 | elif field_to_mod == 'ip_dst': |
| 797 | act = action.action_set_nw_dst() |
| 798 | act.nw_addr = parse.parse_ip(mod_field_vals['ip_dst']) |
| 799 | elif field_to_mod == 'ip_tos': |
| 800 | act = action.action_set_nw_tos() |
| 801 | act.nw_tos = mod_field_vals['ip_tos'] |
| 802 | elif field_to_mod == 'tcp_sport': |
| 803 | act = action.action_set_tp_src() |
| 804 | act.tp_port = mod_field_vals['tcp_sport'] |
| 805 | elif field_to_mod == 'tcp_dport': |
| 806 | act = action.action_set_tp_dst() |
| 807 | act.tp_port = mod_field_vals['tcp_dport'] |
Rich Lane | 110e0e3 | 2012-10-26 16:21:46 -0700 | [diff] [blame] | 808 | elif field_to_mod == 'udp_sport': |
| 809 | act = action.action_set_tp_src() |
| 810 | act.tp_port = mod_field_vals['udp_sport'] |
| 811 | elif field_to_mod == 'udp_dport': |
| 812 | act = action.action_set_tp_dst() |
| 813 | act.tp_port = mod_field_vals['udp_dport'] |
Dan Talayco | 4b2bee6 | 2010-07-20 14:10:05 -0700 | [diff] [blame] | 814 | else: |
| 815 | parent.assertTrue(0, "Unknown field to modify: " + str(field_to_mod)) |
| 816 | |
| 817 | return act |
| 818 | |
| 819 | def pkt_action_setup(parent, start_field_vals={}, mod_field_vals={}, |
Rich Lane | 110e0e3 | 2012-10-26 16:21:46 -0700 | [diff] [blame] | 820 | mod_fields=[], tp="tcp", check_test_params=False): |
Dan Talayco | 4b2bee6 | 2010-07-20 14:10:05 -0700 | [diff] [blame] | 821 | """ |
| 822 | Set up the ingress and expected packet and action list for a test |
| 823 | |
Rich Lane | 2014f9b | 2012-10-05 15:29:40 -0700 | [diff] [blame] | 824 | @param parent Must implement assertTrue |
Dan Talayco | 4b2bee6 | 2010-07-20 14:10:05 -0700 | [diff] [blame] | 825 | @param start_field_values Field values to use for ingress packet (optional) |
| 826 | @param mod_field_values Field values to use for modified packet (optional) |
| 827 | @param mod_fields The list of fields to be modified by the switch in the test. |
| 828 | @params check_test_params If True, will check the parameters vid, add_vlan |
| 829 | and strip_vlan from the command line. |
| 830 | |
| 831 | Returns a triple: pkt-to-send, expected-pkt, action-list |
| 832 | """ |
| 833 | |
| 834 | new_actions = [] |
| 835 | |
Dan Talayco | 4b2bee6 | 2010-07-20 14:10:05 -0700 | [diff] [blame] | 836 | base_pkt_params = {} |
| 837 | base_pkt_params['pktlen'] = 100 |
| 838 | base_pkt_params['dl_dst'] = '00:DE:F0:12:34:56' |
| 839 | base_pkt_params['dl_src'] = '00:23:45:67:89:AB' |
| 840 | base_pkt_params['dl_vlan_enable'] = False |
| 841 | base_pkt_params['dl_vlan'] = 2 |
| 842 | base_pkt_params['dl_vlan_pcp'] = 0 |
| 843 | base_pkt_params['ip_src'] = '192.168.0.1' |
| 844 | base_pkt_params['ip_dst'] = '192.168.0.2' |
| 845 | base_pkt_params['ip_tos'] = 0 |
Rich Lane | 110e0e3 | 2012-10-26 16:21:46 -0700 | [diff] [blame] | 846 | if tp == "tcp": |
| 847 | base_pkt_params['tcp_sport'] = 1234 |
| 848 | base_pkt_params['tcp_dport'] = 80 |
| 849 | elif tp == "udp": |
| 850 | base_pkt_params['udp_sport'] = 1234 |
| 851 | base_pkt_params['udp_dport'] = 80 |
Dan Talayco | 4b2bee6 | 2010-07-20 14:10:05 -0700 | [diff] [blame] | 852 | for keyname in start_field_vals.keys(): |
| 853 | base_pkt_params[keyname] = start_field_vals[keyname] |
| 854 | |
| 855 | mod_pkt_params = {} |
| 856 | mod_pkt_params['pktlen'] = 100 |
| 857 | mod_pkt_params['dl_dst'] = '00:21:0F:ED:CB:A9' |
| 858 | mod_pkt_params['dl_src'] = '00:ED:CB:A9:87:65' |
| 859 | mod_pkt_params['dl_vlan_enable'] = False |
| 860 | mod_pkt_params['dl_vlan'] = 3 |
| 861 | mod_pkt_params['dl_vlan_pcp'] = 7 |
| 862 | mod_pkt_params['ip_src'] = '10.20.30.40' |
| 863 | mod_pkt_params['ip_dst'] = '50.60.70.80' |
| 864 | mod_pkt_params['ip_tos'] = 0xf0 |
Rich Lane | 110e0e3 | 2012-10-26 16:21:46 -0700 | [diff] [blame] | 865 | if tp == "tcp": |
| 866 | mod_pkt_params['tcp_sport'] = 4321 |
| 867 | mod_pkt_params['tcp_dport'] = 8765 |
| 868 | elif tp == "udp": |
| 869 | mod_pkt_params['udp_sport'] = 4321 |
| 870 | mod_pkt_params['udp_dport'] = 8765 |
Dan Talayco | 4b2bee6 | 2010-07-20 14:10:05 -0700 | [diff] [blame] | 871 | for keyname in mod_field_vals.keys(): |
| 872 | mod_pkt_params[keyname] = mod_field_vals[keyname] |
| 873 | |
| 874 | # Check for test param modifications |
| 875 | strip = False |
| 876 | if check_test_params: |
Rich Lane | 2014f9b | 2012-10-05 15:29:40 -0700 | [diff] [blame] | 877 | add_vlan = test_param_get('add_vlan') |
| 878 | strip_vlan = test_param_get('strip_vlan') |
| 879 | vid = test_param_get('vid') |
Dan Talayco | 4b2bee6 | 2010-07-20 14:10:05 -0700 | [diff] [blame] | 880 | |
| 881 | if add_vlan and strip_vlan: |
| 882 | parent.assertTrue(0, "Add and strip VLAN both specified") |
| 883 | |
| 884 | if vid: |
| 885 | base_pkt_params['dl_vlan_enable'] = True |
| 886 | base_pkt_params['dl_vlan'] = vid |
| 887 | if 'dl_vlan' in mod_fields: |
| 888 | mod_pkt_params['dl_vlan'] = vid + 1 |
| 889 | |
| 890 | if add_vlan: |
| 891 | base_pkt_params['dl_vlan_enable'] = False |
| 892 | mod_pkt_params['dl_vlan_enable'] = True |
| 893 | mod_pkt_params['pktlen'] = base_pkt_params['pktlen'] + 4 |
| 894 | mod_fields.append('pktlen') |
| 895 | mod_fields.append('dl_vlan_enable') |
| 896 | if 'dl_vlan' not in mod_fields: |
| 897 | mod_fields.append('dl_vlan') |
| 898 | elif strip_vlan: |
| 899 | base_pkt_params['dl_vlan_enable'] = True |
| 900 | mod_pkt_params['dl_vlan_enable'] = False |
| 901 | mod_pkt_params['pktlen'] = base_pkt_params['pktlen'] - 4 |
| 902 | mod_fields.append('dl_vlan_enable') |
| 903 | mod_fields.append('pktlen') |
| 904 | |
Rich Lane | 110e0e3 | 2012-10-26 16:21:46 -0700 | [diff] [blame] | 905 | if tp == "tcp": |
| 906 | packet_builder = simple_tcp_packet |
| 907 | elif tp == "udp": |
| 908 | packet_builder = simple_udp_packet |
| 909 | else: |
| 910 | raise NotImplementedError("unknown transport protocol %s" % tp) |
| 911 | |
Dan Talayco | 4b2bee6 | 2010-07-20 14:10:05 -0700 | [diff] [blame] | 912 | # Build the ingress packet |
Rich Lane | 110e0e3 | 2012-10-26 16:21:46 -0700 | [diff] [blame] | 913 | ingress_pkt = packet_builder(**base_pkt_params) |
Dan Talayco | 4b2bee6 | 2010-07-20 14:10:05 -0700 | [diff] [blame] | 914 | |
| 915 | # Build the expected packet, modifying the indicated fields |
| 916 | for item in mod_fields: |
| 917 | base_pkt_params[item] = mod_pkt_params[item] |
| 918 | act = action_generate(parent, item, mod_pkt_params) |
| 919 | if act: |
| 920 | new_actions.append(act) |
| 921 | |
Rich Lane | 110e0e3 | 2012-10-26 16:21:46 -0700 | [diff] [blame] | 922 | expected_pkt = packet_builder(**base_pkt_params) |
Dan Talayco | 4b2bee6 | 2010-07-20 14:10:05 -0700 | [diff] [blame] | 923 | |
| 924 | return (ingress_pkt, expected_pkt, new_actions) |
Dan Talayco | 677c0b7 | 2011-08-23 22:53:38 -0700 | [diff] [blame] | 925 | |
| 926 | # Generate a simple "drop" flow mod |
| 927 | # If in_band is true, then only drop from first test port |
| 928 | def flow_mod_gen(port_map, in_band): |
| 929 | request = message.flow_mod() |
| 930 | request.match.wildcards = ofp.OFPFW_ALL |
| 931 | if in_band: |
| 932 | request.match.wildcards = ofp.OFPFW_ALL - ofp.OFPFW_IN_PORT |
| 933 | for of_port, ifname in port_map.items(): # Grab first port |
| 934 | break |
| 935 | request.match.in_port = of_port |
| 936 | request.buffer_id = 0xffffffff |
| 937 | return request |
Dan Talayco | ba3745c | 2010-07-21 21:51:08 -0700 | [diff] [blame] | 938 | |
| 939 | def skip_message_emit(parent, s): |
| 940 | """ |
| 941 | Print out a 'skipped' message to stderr |
| 942 | |
| 943 | @param s The string to print out to the log file |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame] | 944 | @param parent Must implement config object |
Dan Talayco | ba3745c | 2010-07-21 21:51:08 -0700 | [diff] [blame] | 945 | """ |
| 946 | global skipped_test_count |
| 947 | |
| 948 | skipped_test_count += 1 |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame] | 949 | logging.info("Skipping: " + s) |
Rich Lane | 477f481 | 2012-10-04 22:49:00 -0700 | [diff] [blame] | 950 | if config["dbg_level"] < logging.WARNING: |
Dan Talayco | ba3745c | 2010-07-21 21:51:08 -0700 | [diff] [blame] | 951 | sys.stderr.write("(skipped) ") |
| 952 | else: |
| 953 | sys.stderr.write("(S)") |
Dan Talayco | 677c0b7 | 2011-08-23 22:53:38 -0700 | [diff] [blame] | 954 | |
Dan Talayco | 8a64e33 | 2012-03-28 14:53:20 -0700 | [diff] [blame] | 955 | |
| 956 | def all_stats_get(parent): |
| 957 | """ |
| 958 | Get the aggregate stats for all flows in the table |
| 959 | @param parent Test instance with controller connection and assert |
| 960 | @returns dict with keys flows, packets, bytes, active (flows), |
| 961 | lookups, matched |
| 962 | """ |
| 963 | stat_req = message.aggregate_stats_request() |
| 964 | stat_req.match = ofp.ofp_match() |
| 965 | stat_req.match.wildcards = ofp.OFPFW_ALL |
| 966 | stat_req.table_id = 0xff |
| 967 | stat_req.out_port = ofp.OFPP_NONE |
| 968 | |
| 969 | rv = {} |
| 970 | |
Rich Lane | c8aaa3e | 2012-07-26 19:28:02 -0700 | [diff] [blame] | 971 | (reply, pkt) = parent.controller.transact(stat_req) |
Dan Talayco | 8a64e33 | 2012-03-28 14:53:20 -0700 | [diff] [blame] | 972 | parent.assertTrue(len(reply.stats) == 1, "Did not receive flow stats reply") |
| 973 | |
| 974 | for obj in reply.stats: |
| 975 | (rv["flows"], rv["packets"], rv["bytes"]) = (obj.flow_count, |
| 976 | obj.packet_count, obj.byte_count) |
| 977 | break |
| 978 | |
| 979 | request = message.table_stats_request() |
Rich Lane | c8aaa3e | 2012-07-26 19:28:02 -0700 | [diff] [blame] | 980 | (reply , pkt) = parent.controller.transact(request) |
Dan Talayco | 8a64e33 | 2012-03-28 14:53:20 -0700 | [diff] [blame] | 981 | |
| 982 | |
| 983 | (rv["active"], rv["lookups"], rv["matched"]) = (0,0,0) |
| 984 | for obj in reply.stats: |
| 985 | rv["active"] += obj.active_count |
| 986 | rv["lookups"] += obj.lookup_count |
| 987 | rv["matched"] += obj.matched_count |
| 988 | |
| 989 | return rv |
Dan Talayco | 2baf8b5 | 2012-03-30 09:55:42 -0700 | [diff] [blame] | 990 | |
| 991 | FILTER=''.join([(len(repr(chr(x)))==3) and chr(x) or '.' |
| 992 | for x in range(256)]) |
| 993 | |
| 994 | def hex_dump_buffer(src, length=16): |
| 995 | """ |
| 996 | Convert src to a hex dump string and return the string |
| 997 | @param src The source buffer |
| 998 | @param length The number of bytes shown in each line |
| 999 | @returns A string showing the hex dump |
| 1000 | """ |
Dan Talayco | c516fa0 | 2012-04-12 22:28:43 -0700 | [diff] [blame] | 1001 | result = ["\n"] |
Dan Talayco | 2baf8b5 | 2012-03-30 09:55:42 -0700 | [diff] [blame] | 1002 | for i in xrange(0, len(src), length): |
| 1003 | chars = src[i:i+length] |
| 1004 | hex = ' '.join(["%02x" % ord(x) for x in chars]) |
| 1005 | printable = ''.join(["%s" % ((ord(x) <= 127 and |
| 1006 | FILTER[ord(x)]) or '.') for x in chars]) |
| 1007 | result.append("%04x %-*s %s\n" % (i, length*3, hex, printable)) |
| 1008 | return ''.join(result) |
| 1009 | |
| 1010 | def format_packet(pkt): |
| 1011 | return "Packet length %d \n%s" % (len(str(pkt)), |
| 1012 | hex_dump_buffer(str(pkt))) |
Rich Lane | 5d7e89a | 2012-10-26 16:43:13 -0700 | [diff] [blame] | 1013 | |
| 1014 | def inspect_packet(pkt): |
| 1015 | """ |
| 1016 | Wrapper around scapy's show() method. |
| 1017 | @returns A string showing the dissected packet. |
| 1018 | """ |
| 1019 | from cStringIO import StringIO |
| 1020 | out = None |
| 1021 | backup = sys.stdout |
| 1022 | try: |
| 1023 | sys.stdout = StringIO() |
| 1024 | pkt.show2() |
| 1025 | out = sys.stdout.getvalue() |
| 1026 | sys.stdout.close() |
| 1027 | finally: |
| 1028 | sys.stdout = backup |
| 1029 | return out |