Dan Talayco | b66b112 | 2010-02-10 22:38:49 -0800 | [diff] [blame] | 1 | """ |
| 2 | OpenFlow message parsing functions |
| 3 | """ |
| 4 | |
| 5 | import sys |
Dan Talayco | 235d7cb | 2010-03-12 10:03:18 -0800 | [diff] [blame] | 6 | import logging |
Rich Lane | ed1fa2d | 2013-01-08 13:23:37 -0800 | [diff] [blame] | 7 | import message |
| 8 | import error |
| 9 | import action |
| 10 | import cstruct |
Dan Talayco | f75360a | 2010-02-05 22:22:54 -0800 | [diff] [blame] | 11 | from action_list import action_list |
Dan Talayco | b66b112 | 2010-02-10 22:38:49 -0800 | [diff] [blame] | 12 | try: |
Dan Talayco | 958f3b9 | 2010-03-12 21:58:57 -0800 | [diff] [blame] | 13 | import scapy.all as scapy |
Dan Talayco | b66b112 | 2010-02-10 22:38:49 -0800 | [diff] [blame] | 14 | except: |
Dan Talayco | d2ca103 | 2010-03-10 14:40:26 -0800 | [diff] [blame] | 15 | try: |
Dan Talayco | 958f3b9 | 2010-03-12 21:58:57 -0800 | [diff] [blame] | 16 | import scapy as scapy |
Dan Talayco | d2ca103 | 2010-03-10 14:40:26 -0800 | [diff] [blame] | 17 | except: |
| 18 | sys.exit("Need to install scapy for packet parsing") |
Dan Talayco | f75360a | 2010-02-05 22:22:54 -0800 | [diff] [blame] | 19 | |
| 20 | """ |
| 21 | of_message.py |
| 22 | Contains wrapper functions and classes for the of_message namespace |
| 23 | that are generated by hand. It includes the rest of the wrapper |
| 24 | function information into the of_message namespace |
| 25 | """ |
| 26 | |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 27 | parse_logger = logging.getLogger("parse") |
| 28 | #parse_logger.setLevel(logging.DEBUG) |
Dan Talayco | 08d9dfe | 2010-02-12 23:02:11 -0800 | [diff] [blame] | 29 | |
Dan Talayco | f75360a | 2010-02-05 22:22:54 -0800 | [diff] [blame] | 30 | # These message types are subclassed |
| 31 | msg_type_subclassed = [ |
Rich Lane | ed1fa2d | 2013-01-08 13:23:37 -0800 | [diff] [blame] | 32 | cstruct.OFPT_STATS_REQUEST, |
| 33 | cstruct.OFPT_STATS_REPLY, |
| 34 | cstruct.OFPT_ERROR |
Dan Talayco | f75360a | 2010-02-05 22:22:54 -0800 | [diff] [blame] | 35 | ] |
| 36 | |
| 37 | # Maps from sub-types to classes |
| 38 | stats_reply_to_class_map = { |
Rich Lane | ed1fa2d | 2013-01-08 13:23:37 -0800 | [diff] [blame] | 39 | cstruct.OFPST_DESC : message.desc_stats_reply, |
| 40 | cstruct.OFPST_AGGREGATE : message.aggregate_stats_reply, |
| 41 | cstruct.OFPST_FLOW : message.flow_stats_reply, |
| 42 | cstruct.OFPST_TABLE : message.table_stats_reply, |
| 43 | cstruct.OFPST_PORT : message.port_stats_reply, |
| 44 | cstruct.OFPST_QUEUE : message.queue_stats_reply |
Dan Talayco | f75360a | 2010-02-05 22:22:54 -0800 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | stats_request_to_class_map = { |
Rich Lane | ed1fa2d | 2013-01-08 13:23:37 -0800 | [diff] [blame] | 48 | cstruct.OFPST_DESC : message.desc_stats_request, |
| 49 | cstruct.OFPST_AGGREGATE : message.aggregate_stats_request, |
| 50 | cstruct.OFPST_FLOW : message.flow_stats_request, |
| 51 | cstruct.OFPST_TABLE : message.table_stats_request, |
| 52 | cstruct.OFPST_PORT : message.port_stats_request, |
| 53 | cstruct.OFPST_QUEUE : message.queue_stats_request |
Dan Talayco | f75360a | 2010-02-05 22:22:54 -0800 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | error_to_class_map = { |
Rich Lane | ed1fa2d | 2013-01-08 13:23:37 -0800 | [diff] [blame] | 57 | cstruct.OFPET_HELLO_FAILED : message.hello_failed_error_msg, |
| 58 | cstruct.OFPET_BAD_REQUEST : message.bad_request_error_msg, |
| 59 | cstruct.OFPET_BAD_ACTION : message.bad_action_error_msg, |
| 60 | cstruct.OFPET_FLOW_MOD_FAILED : message.flow_mod_failed_error_msg, |
| 61 | cstruct.OFPET_PORT_MOD_FAILED : message.port_mod_failed_error_msg, |
| 62 | cstruct.OFPET_QUEUE_OP_FAILED : message.queue_op_failed_error_msg |
Dan Talayco | f75360a | 2010-02-05 22:22:54 -0800 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | # Map from header type value to the underlieing message class |
| 66 | msg_type_to_class_map = { |
Rich Lane | ed1fa2d | 2013-01-08 13:23:37 -0800 | [diff] [blame] | 67 | cstruct.OFPT_HELLO : message.hello, |
| 68 | cstruct.OFPT_ERROR : message.error, |
| 69 | cstruct.OFPT_ECHO_REQUEST : message.echo_request, |
| 70 | cstruct.OFPT_ECHO_REPLY : message.echo_reply, |
| 71 | cstruct.OFPT_VENDOR : message.vendor, |
| 72 | cstruct.OFPT_FEATURES_REQUEST : message.features_request, |
| 73 | cstruct.OFPT_FEATURES_REPLY : message.features_reply, |
| 74 | cstruct.OFPT_GET_CONFIG_REQUEST : message.get_config_request, |
| 75 | cstruct.OFPT_GET_CONFIG_REPLY : message.get_config_reply, |
| 76 | cstruct.OFPT_SET_CONFIG : message.set_config, |
| 77 | cstruct.OFPT_PACKET_IN : message.packet_in, |
| 78 | cstruct.OFPT_FLOW_REMOVED : message.flow_removed, |
| 79 | cstruct.OFPT_PORT_STATUS : message.port_status, |
| 80 | cstruct.OFPT_PACKET_OUT : message.packet_out, |
| 81 | cstruct.OFPT_FLOW_MOD : message.flow_mod, |
| 82 | cstruct.OFPT_PORT_MOD : message.port_mod, |
| 83 | cstruct.OFPT_STATS_REQUEST : message.stats_request, |
| 84 | cstruct.OFPT_STATS_REPLY : message.stats_reply, |
| 85 | cstruct.OFPT_BARRIER_REQUEST : message.barrier_request, |
| 86 | cstruct.OFPT_BARRIER_REPLY : message.barrier_reply, |
| 87 | cstruct.OFPT_QUEUE_GET_CONFIG_REQUEST : message.queue_get_config_request, |
| 88 | cstruct.OFPT_QUEUE_GET_CONFIG_REPLY : message.queue_get_config_reply |
Dan Talayco | f75360a | 2010-02-05 22:22:54 -0800 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | def _of_message_to_object(binary_string): |
| 92 | """ |
| 93 | Map a binary string to the corresponding class. |
| 94 | |
| 95 | Appropriately resolves subclasses |
| 96 | """ |
Rich Lane | ed1fa2d | 2013-01-08 13:23:37 -0800 | [diff] [blame] | 97 | hdr = message.ofp_header() |
Dan Talayco | f75360a | 2010-02-05 22:22:54 -0800 | [diff] [blame] | 98 | hdr.unpack(binary_string) |
| 99 | # FIXME: Add error detection |
| 100 | if not hdr.type in msg_type_subclassed: |
| 101 | return msg_type_to_class_map[hdr.type]() |
Rich Lane | ed1fa2d | 2013-01-08 13:23:37 -0800 | [diff] [blame] | 102 | if hdr.type == cstruct.OFPT_STATS_REQUEST: |
| 103 | sub_hdr = message.ofp_stats_request() |
| 104 | sub_hdr.unpack(binary_string[cstruct.OFP_HEADER_BYTES:]) |
Dan Talayco | b66b112 | 2010-02-10 22:38:49 -0800 | [diff] [blame] | 105 | try: |
| 106 | obj = stats_request_to_class_map[sub_hdr.type]() |
| 107 | except KeyError: |
| 108 | obj = None |
| 109 | return obj |
Rich Lane | ed1fa2d | 2013-01-08 13:23:37 -0800 | [diff] [blame] | 110 | elif hdr.type == cstruct.OFPT_STATS_REPLY: |
| 111 | sub_hdr = message.ofp_stats_reply() |
| 112 | sub_hdr.unpack(binary_string[cstruct.OFP_HEADER_BYTES:]) |
Dan Talayco | b66b112 | 2010-02-10 22:38:49 -0800 | [diff] [blame] | 113 | try: |
| 114 | obj = stats_reply_to_class_map[sub_hdr.type]() |
| 115 | except KeyError: |
| 116 | obj = None |
| 117 | return obj |
Rich Lane | ed1fa2d | 2013-01-08 13:23:37 -0800 | [diff] [blame] | 118 | elif hdr.type == cstruct.OFPT_ERROR: |
| 119 | sub_hdr = message.ofp_error_msg() |
| 120 | sub_hdr.unpack(binary_string[cstruct.OFP_HEADER_BYTES:]) |
Dan Talayco | b9cb548 | 2010-02-09 15:23:12 -0800 | [diff] [blame] | 121 | return error_to_class_map[sub_hdr.type]() |
Dan Talayco | f75360a | 2010-02-05 22:22:54 -0800 | [diff] [blame] | 122 | else: |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 123 | parse_logger.error("Cannot parse pkt to message") |
Dan Talayco | f75360a | 2010-02-05 22:22:54 -0800 | [diff] [blame] | 124 | return None |
| 125 | |
| 126 | def of_message_parse(binary_string, raw=False): |
| 127 | """ |
| 128 | Parse an OpenFlow packet |
| 129 | |
| 130 | Parses a raw OpenFlow packet into a Python class, with class |
| 131 | members fully populated. |
| 132 | |
| 133 | @param binary_string The packet (string) to be parsed |
Dan Talayco | f75360a | 2010-02-05 22:22:54 -0800 | [diff] [blame] | 134 | @param raw If true, interpret the packet as an L2 packet. Not |
| 135 | yet supported. |
Dan Talayco | f75360a | 2010-02-05 22:22:54 -0800 | [diff] [blame] | 136 | @return An object of some message class or None if fails |
Dan Talayco | 08d9dfe | 2010-02-12 23:02:11 -0800 | [diff] [blame] | 137 | Note that any data beyond that parsed is not returned |
Dan Talayco | f75360a | 2010-02-05 22:22:54 -0800 | [diff] [blame] | 138 | |
| 139 | """ |
| 140 | |
| 141 | if raw: |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 142 | parse_logger.error("raw packet message parsing not supported") |
Dan Talayco | f75360a | 2010-02-05 22:22:54 -0800 | [diff] [blame] | 143 | return None |
| 144 | |
| 145 | obj = _of_message_to_object(binary_string) |
Dan Talayco | b66b112 | 2010-02-10 22:38:49 -0800 | [diff] [blame] | 146 | if obj: |
Dan Talayco | f75360a | 2010-02-05 22:22:54 -0800 | [diff] [blame] | 147 | obj.unpack(binary_string) |
| 148 | return obj |
| 149 | |
Dan Talayco | b9cb548 | 2010-02-09 15:23:12 -0800 | [diff] [blame] | 150 | |
| 151 | def of_header_parse(binary_string, raw=False): |
| 152 | """ |
| 153 | Parse only the header from an OpenFlow packet |
| 154 | |
| 155 | Parses the header from a raw OpenFlow packet into a |
| 156 | an ofp_header Python class. |
| 157 | |
| 158 | @param binary_string The packet (string) to be parsed |
| 159 | @param raw If true, interpret the packet as an L2 packet. Not |
| 160 | yet supported. |
| 161 | @return An ofp_header object |
| 162 | |
| 163 | """ |
| 164 | |
| 165 | if raw: |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 166 | parse_logger.error("raw packet message parsing not supported") |
Dan Talayco | b9cb548 | 2010-02-09 15:23:12 -0800 | [diff] [blame] | 167 | return None |
| 168 | |
Rich Lane | ed1fa2d | 2013-01-08 13:23:37 -0800 | [diff] [blame] | 169 | hdr = message.ofp_header() |
Dan Talayco | b9cb548 | 2010-02-09 15:23:12 -0800 | [diff] [blame] | 170 | hdr.unpack(binary_string) |
| 171 | |
| 172 | return hdr |
| 173 | |
Dan Talayco | b66b112 | 2010-02-10 22:38:49 -0800 | [diff] [blame] | 174 | map_wc_field_to_match_member = { |
| 175 | 'OFPFW_DL_VLAN' : 'dl_vlan', |
| 176 | 'OFPFW_DL_SRC' : 'dl_src', |
| 177 | 'OFPFW_DL_DST' : 'dl_dst', |
| 178 | 'OFPFW_DL_TYPE' : 'dl_type', |
| 179 | 'OFPFW_NW_PROTO' : 'nw_proto', |
| 180 | 'OFPFW_TP_SRC' : 'tp_src', |
| 181 | 'OFPFW_TP_DST' : 'tp_dst', |
| 182 | 'OFPFW_NW_SRC_SHIFT' : 'nw_src_shift', |
| 183 | 'OFPFW_NW_SRC_BITS' : 'nw_src_bits', |
| 184 | 'OFPFW_NW_SRC_MASK' : 'nw_src_mask', |
| 185 | 'OFPFW_NW_SRC_ALL' : 'nw_src_all', |
| 186 | 'OFPFW_NW_DST_SHIFT' : 'nw_dst_shift', |
| 187 | 'OFPFW_NW_DST_BITS' : 'nw_dst_bits', |
| 188 | 'OFPFW_NW_DST_MASK' : 'nw_dst_mask', |
| 189 | 'OFPFW_NW_DST_ALL' : 'nw_dst_all', |
| 190 | 'OFPFW_DL_VLAN_PCP' : 'dl_vlan_pcp', |
| 191 | 'OFPFW_NW_TOS' : 'nw_tos' |
| 192 | } |
| 193 | |
| 194 | |
| 195 | def parse_mac(mac_str): |
Dan Talayco | b9cb548 | 2010-02-09 15:23:12 -0800 | [diff] [blame] | 196 | """ |
Dan Talayco | b66b112 | 2010-02-10 22:38:49 -0800 | [diff] [blame] | 197 | Parse a MAC address |
| 198 | |
| 199 | Parse a MAC address ':' separated string of hex digits to an |
| 200 | array of integer values. '00:d0:05:5d:24:00' => [0, 208, 5, 93, 36, 0] |
| 201 | @param mac_str The string to convert |
| 202 | @return Array of 6 integer values |
| 203 | """ |
Rich Lane | a92f252 | 2012-10-04 18:11:04 -0700 | [diff] [blame] | 204 | return map(lambda val: int(val, 16), mac_str.split(":")) |
Dan Talayco | b66b112 | 2010-02-10 22:38:49 -0800 | [diff] [blame] | 205 | |
| 206 | def parse_ip(ip_str): |
| 207 | """ |
| 208 | Parse an IP address |
| 209 | |
| 210 | Parse an IP address '.' separated string of decimal digits to an |
| 211 | host ordered integer. '172.24.74.77' => |
| 212 | @param ip_str The string to convert |
| 213 | @return Integer value |
| 214 | """ |
Rich Lane | a92f252 | 2012-10-04 18:11:04 -0700 | [diff] [blame] | 215 | array = map(lambda val: int(val), ip_str.split(".")) |
Dan Talayco | b66b112 | 2010-02-10 22:38:49 -0800 | [diff] [blame] | 216 | val = 0 |
| 217 | for a in array: |
| 218 | val <<= 8 |
| 219 | val += a |
| 220 | return val |
| 221 | |
| 222 | def packet_type_classify(ether): |
| 223 | try: |
Dan Talayco | 958f3b9 | 2010-03-12 21:58:57 -0800 | [diff] [blame] | 224 | dot1q = ether[scapy.Dot1Q] |
Dan Talayco | b66b112 | 2010-02-10 22:38:49 -0800 | [diff] [blame] | 225 | except: |
| 226 | dot1q = None |
Dan Talayco | 08d9dfe | 2010-02-12 23:02:11 -0800 | [diff] [blame] | 227 | |
Dan Talayco | b66b112 | 2010-02-10 22:38:49 -0800 | [diff] [blame] | 228 | try: |
Dan Talayco | 958f3b9 | 2010-03-12 21:58:57 -0800 | [diff] [blame] | 229 | ip = ether[scapy.IP] |
Dan Talayco | b66b112 | 2010-02-10 22:38:49 -0800 | [diff] [blame] | 230 | except: |
| 231 | ip = None |
Dan Talayco | 08d9dfe | 2010-02-12 23:02:11 -0800 | [diff] [blame] | 232 | |
Dan Talayco | b66b112 | 2010-02-10 22:38:49 -0800 | [diff] [blame] | 233 | try: |
Dan Talayco | 958f3b9 | 2010-03-12 21:58:57 -0800 | [diff] [blame] | 234 | tcp = ether[scapy.TCP] |
Dan Talayco | b66b112 | 2010-02-10 22:38:49 -0800 | [diff] [blame] | 235 | except: |
| 236 | tcp = None |
Dan Talayco | 08d9dfe | 2010-02-12 23:02:11 -0800 | [diff] [blame] | 237 | |
Dan Talayco | b66b112 | 2010-02-10 22:38:49 -0800 | [diff] [blame] | 238 | try: |
Dan Talayco | 958f3b9 | 2010-03-12 21:58:57 -0800 | [diff] [blame] | 239 | udp = ether[scapy.UDP] |
Dan Talayco | b66b112 | 2010-02-10 22:38:49 -0800 | [diff] [blame] | 240 | except: |
| 241 | udp = None |
| 242 | |
Tatsuya Yabe | b8fb3c3 | 2010-06-14 15:48:36 -0700 | [diff] [blame] | 243 | try: |
| 244 | icmp = ether[scapy.ICMP] |
| 245 | except: |
| 246 | icmp = None |
| 247 | |
Christian Dickmann | 8b59b4b | 2012-09-23 16:48:30 -0700 | [diff] [blame] | 248 | try: |
| 249 | arp = ether[scapy.ARP] |
| 250 | except: |
| 251 | arp = None |
Dan Talayco | b66b112 | 2010-02-10 22:38:49 -0800 | [diff] [blame] | 252 | return (dot1q, ip, tcp, udp, icmp, arp) |
| 253 | |
| 254 | def packet_to_flow_match(packet, pkt_format="L2"): |
| 255 | """ |
| 256 | Create a flow match that matches packet with the given wildcards |
Dan Talayco | b9cb548 | 2010-02-09 15:23:12 -0800 | [diff] [blame] | 257 | |
| 258 | @param packet The packet to use as a flow template |
Dan Talayco | a3b2018 | 2010-02-10 22:49:34 -0800 | [diff] [blame] | 259 | @param pkt_format Currently only L2 is supported. Will indicate the |
| 260 | overall packet type for parsing |
Dan Talayco | b66b112 | 2010-02-10 22:38:49 -0800 | [diff] [blame] | 261 | @return An ofp_match object if successful. None if format is not |
| 262 | recognized. The wildcards of the match will be cleared for the |
| 263 | values extracted from the packet. |
| 264 | |
Dan Talayco | a3b2018 | 2010-02-10 22:49:34 -0800 | [diff] [blame] | 265 | @todo check min length of packet |
| 266 | @todo Check if packet is other than L2 format |
| 267 | @todo Implement ICMP and ARP fields |
Dan Talayco | b9cb548 | 2010-02-09 15:23:12 -0800 | [diff] [blame] | 268 | """ |
| 269 | |
Dan Talayco | b66b112 | 2010-02-10 22:38:49 -0800 | [diff] [blame] | 270 | #@todo check min length of packet |
Dan Talayco | b66b112 | 2010-02-10 22:38:49 -0800 | [diff] [blame] | 271 | if pkt_format.upper() != "L2": |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 272 | parse_logger.error("Only L2 supported for packet_to_flow") |
Dan Talayco | b66b112 | 2010-02-10 22:38:49 -0800 | [diff] [blame] | 273 | return None |
| 274 | |
Dan Talayco | cb6b5d7 | 2010-03-10 13:59:33 -0800 | [diff] [blame] | 275 | if type(packet) == type(""): |
Dan Talayco | 958f3b9 | 2010-03-12 21:58:57 -0800 | [diff] [blame] | 276 | ether = scapy.Ether(packet) |
Dan Talayco | cb6b5d7 | 2010-03-10 13:59:33 -0800 | [diff] [blame] | 277 | else: |
| 278 | ether = packet |
| 279 | |
Dan Talayco | b66b112 | 2010-02-10 22:38:49 -0800 | [diff] [blame] | 280 | # For now, assume ether IP packet and ignore wildcards |
Dan Talayco | cb6b5d7 | 2010-03-10 13:59:33 -0800 | [diff] [blame] | 281 | try: |
| 282 | (dot1q, ip, tcp, udp, icmp, arp) = packet_type_classify(ether) |
| 283 | except: |
| 284 | parse_logger.error("packet_to_flow_match: Classify error") |
| 285 | return None |
Dan Talayco | b66b112 | 2010-02-10 22:38:49 -0800 | [diff] [blame] | 286 | |
Rich Lane | ed1fa2d | 2013-01-08 13:23:37 -0800 | [diff] [blame] | 287 | match = cstruct.ofp_match() |
| 288 | match.wildcards = cstruct.OFPFW_ALL |
Dan Talayco | b66b112 | 2010-02-10 22:38:49 -0800 | [diff] [blame] | 289 | #@todo Check if packet is other than L2 format |
| 290 | match.dl_dst = parse_mac(ether.dst) |
Rich Lane | ed1fa2d | 2013-01-08 13:23:37 -0800 | [diff] [blame] | 291 | match.wildcards &= ~cstruct.OFPFW_DL_DST |
Dan Talayco | b66b112 | 2010-02-10 22:38:49 -0800 | [diff] [blame] | 292 | match.dl_src = parse_mac(ether.src) |
Rich Lane | ed1fa2d | 2013-01-08 13:23:37 -0800 | [diff] [blame] | 293 | match.wildcards &= ~cstruct.OFPFW_DL_SRC |
Dan Talayco | b66b112 | 2010-02-10 22:38:49 -0800 | [diff] [blame] | 294 | match.dl_type = ether.type |
Rich Lane | ed1fa2d | 2013-01-08 13:23:37 -0800 | [diff] [blame] | 295 | match.wildcards &= ~cstruct.OFPFW_DL_TYPE |
Dan Talayco | b66b112 | 2010-02-10 22:38:49 -0800 | [diff] [blame] | 296 | |
| 297 | if dot1q: |
| 298 | match.dl_vlan = dot1q.vlan |
Dan Talayco | b66b112 | 2010-02-10 22:38:49 -0800 | [diff] [blame] | 299 | match.dl_vlan_pcp = dot1q.prio |
Dan Talayco | 8dbc4d7 | 2010-07-17 00:32:46 -0700 | [diff] [blame] | 300 | match.dl_type = dot1q.type |
Tatsuya Yabe | b8fb3c3 | 2010-06-14 15:48:36 -0700 | [diff] [blame] | 301 | else: |
Rich Lane | ed1fa2d | 2013-01-08 13:23:37 -0800 | [diff] [blame] | 302 | match.dl_vlan = cstruct.OFP_VLAN_NONE |
Tatsuya Yabe | b8fb3c3 | 2010-06-14 15:48:36 -0700 | [diff] [blame] | 303 | match.dl_vlan_pcp = 0 |
Rich Lane | ed1fa2d | 2013-01-08 13:23:37 -0800 | [diff] [blame] | 304 | match.wildcards &= ~cstruct.OFPFW_DL_VLAN |
| 305 | match.wildcards &= ~cstruct.OFPFW_DL_VLAN_PCP |
Dan Talayco | b66b112 | 2010-02-10 22:38:49 -0800 | [diff] [blame] | 306 | |
| 307 | if ip: |
| 308 | match.nw_src = parse_ip(ip.src) |
Rich Lane | ed1fa2d | 2013-01-08 13:23:37 -0800 | [diff] [blame] | 309 | match.wildcards &= ~cstruct.OFPFW_NW_SRC_MASK |
Dan Talayco | b66b112 | 2010-02-10 22:38:49 -0800 | [diff] [blame] | 310 | match.nw_dst = parse_ip(ip.dst) |
Rich Lane | ed1fa2d | 2013-01-08 13:23:37 -0800 | [diff] [blame] | 311 | match.wildcards &= ~cstruct.OFPFW_NW_DST_MASK |
Dan Talayco | b66b112 | 2010-02-10 22:38:49 -0800 | [diff] [blame] | 312 | match.nw_tos = ip.tos |
Rich Lane | ed1fa2d | 2013-01-08 13:23:37 -0800 | [diff] [blame] | 313 | match.wildcards &= ~cstruct.OFPFW_NW_TOS |
Dan Talayco | b66b112 | 2010-02-10 22:38:49 -0800 | [diff] [blame] | 314 | |
| 315 | if tcp: |
Dan Talayco | 89d5734 | 2010-06-07 16:24:59 -0700 | [diff] [blame] | 316 | match.nw_proto = 6 |
Rich Lane | ed1fa2d | 2013-01-08 13:23:37 -0800 | [diff] [blame] | 317 | match.wildcards &= ~cstruct.OFPFW_NW_PROTO |
Dan Talayco | 89d5734 | 2010-06-07 16:24:59 -0700 | [diff] [blame] | 318 | elif not tcp and udp: |
| 319 | tcp = udp |
| 320 | match.nw_proto = 17 |
Rich Lane | ed1fa2d | 2013-01-08 13:23:37 -0800 | [diff] [blame] | 321 | match.wildcards &= ~cstruct.OFPFW_NW_PROTO |
Dan Talayco | 89d5734 | 2010-06-07 16:24:59 -0700 | [diff] [blame] | 322 | |
| 323 | if tcp: |
Dan Talayco | b66b112 | 2010-02-10 22:38:49 -0800 | [diff] [blame] | 324 | match.tp_src = tcp.sport |
Rich Lane | ed1fa2d | 2013-01-08 13:23:37 -0800 | [diff] [blame] | 325 | match.wildcards &= ~cstruct.OFPFW_TP_SRC |
Dan Talayco | b66b112 | 2010-02-10 22:38:49 -0800 | [diff] [blame] | 326 | match.tp_dst = tcp.dport |
Rich Lane | ed1fa2d | 2013-01-08 13:23:37 -0800 | [diff] [blame] | 327 | match.wildcards &= ~cstruct.OFPFW_TP_DST |
Dan Talayco | b66b112 | 2010-02-10 22:38:49 -0800 | [diff] [blame] | 328 | |
Tatsuya Yabe | b8fb3c3 | 2010-06-14 15:48:36 -0700 | [diff] [blame] | 329 | if icmp: |
| 330 | match.nw_proto = 1 |
| 331 | match.tp_src = icmp.type |
| 332 | match.tp_dst = icmp.code |
Rich Lane | ed1fa2d | 2013-01-08 13:23:37 -0800 | [diff] [blame] | 333 | match.wildcards &= ~cstruct.OFPFW_NW_PROTO |
Tatsuya Yabe | b8fb3c3 | 2010-06-14 15:48:36 -0700 | [diff] [blame] | 334 | |
Christian Dickmann | 8b59b4b | 2012-09-23 16:48:30 -0700 | [diff] [blame] | 335 | if arp: |
| 336 | match.nw_proto = arp.op |
Rich Lane | ed1fa2d | 2013-01-08 13:23:37 -0800 | [diff] [blame] | 337 | match.wildcards &= ~cstruct.OFPFW_NW_PROTO |
Christian Dickmann | 8b59b4b | 2012-09-23 16:48:30 -0700 | [diff] [blame] | 338 | match.nw_src = parse_ip(arp.psrc) |
Rich Lane | ed1fa2d | 2013-01-08 13:23:37 -0800 | [diff] [blame] | 339 | match.wildcards &= ~cstruct.OFPFW_NW_SRC_MASK |
Christian Dickmann | 8b59b4b | 2012-09-23 16:48:30 -0700 | [diff] [blame] | 340 | match.nw_dst = parse_ip(arp.pdst) |
Rich Lane | ed1fa2d | 2013-01-08 13:23:37 -0800 | [diff] [blame] | 341 | match.wildcards &= ~cstruct.OFPFW_NW_DST_MASK |
Dan Talayco | b66b112 | 2010-02-10 22:38:49 -0800 | [diff] [blame] | 342 | |
| 343 | return match |