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