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