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