blob: 018b70bfe24953b35fe386b8194ca4d9a22b0823 [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:
14 sys.exit("Need to install scapy for packet parsing")
Dan Talaycof75360a2010-02-05 22:22:54 -080015
16"""
17of_message.py
18Contains wrapper functions and classes for the of_message namespace
19that are generated by hand. It includes the rest of the wrapper
20function information into the of_message namespace
21"""
22
Dan Talayco48370102010-03-03 15:17:33 -080023parse_logger = logging.getLogger("parse")
24#parse_logger.setLevel(logging.DEBUG)
Dan Talayco08d9dfe2010-02-12 23:02:11 -080025
Dan Talaycof75360a2010-02-05 22:22:54 -080026# These message types are subclassed
27msg_type_subclassed = [
28 OFPT_STATS_REQUEST,
29 OFPT_STATS_REPLY,
30 OFPT_ERROR
31]
32
33# Maps from sub-types to classes
34stats_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
43stats_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
52error_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
62msg_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
87def _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 Talaycob9cb5482010-02-09 15:23:12 -080099 sub_hdr = ofp_stats_request()
Dan Talaycob66b1122010-02-10 22:38:49 -0800100 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 Talaycof75360a2010-02-05 22:22:54 -0800106 elif hdr.type == OFPT_STATS_REPLY:
Dan Talaycob9cb5482010-02-09 15:23:12 -0800107 sub_hdr = ofp_stats_reply()
Dan Talaycob66b1122010-02-10 22:38:49 -0800108 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 Talaycob9cb5482010-02-09 15:23:12 -0800114 elif hdr.type == OFPT_ERROR:
115 sub_hdr = ofp_error_msg()
Dan Talaycob66b1122010-02-10 22:38:49 -0800116 sub_hdr.unpack(binary_string[OFP_HEADER_BYTES:])
Dan Talaycob9cb5482010-02-09 15:23:12 -0800117 return error_to_class_map[sub_hdr.type]()
Dan Talaycof75360a2010-02-05 22:22:54 -0800118 else:
Dan Talayco48370102010-03-03 15:17:33 -0800119 parse_logger.error("Cannot parse pkt to message")
Dan Talaycof75360a2010-02-05 22:22:54 -0800120 return None
121
122def 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 Talaycof75360a2010-02-05 22:22:54 -0800130 @param raw If true, interpret the packet as an L2 packet. Not
131 yet supported.
Dan Talaycof75360a2010-02-05 22:22:54 -0800132 @return An object of some message class or None if fails
Dan Talayco08d9dfe2010-02-12 23:02:11 -0800133 Note that any data beyond that parsed is not returned
Dan Talaycof75360a2010-02-05 22:22:54 -0800134
135 """
136
137 if raw:
Dan Talayco48370102010-03-03 15:17:33 -0800138 parse_logger.error("raw packet message parsing not supported")
Dan Talaycof75360a2010-02-05 22:22:54 -0800139 return None
140
141 obj = _of_message_to_object(binary_string)
Dan Talaycob66b1122010-02-10 22:38:49 -0800142 if obj:
Dan Talaycof75360a2010-02-05 22:22:54 -0800143 obj.unpack(binary_string)
144 return obj
145
Dan Talaycob9cb5482010-02-09 15:23:12 -0800146
147def 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 Talayco48370102010-03-03 15:17:33 -0800162 parse_logger.error("raw packet message parsing not supported")
Dan Talaycob9cb5482010-02-09 15:23:12 -0800163 return None
164
165 hdr = ofp_header()
166 hdr.unpack(binary_string)
167
168 return hdr
169
Dan Talaycob66b1122010-02-10 22:38:49 -0800170map_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
191def parse_mac(mac_str):
Dan Talaycob9cb5482010-02-09 15:23:12 -0800192 """
Dan Talaycob66b1122010-02-10 22:38:49 -0800193 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
202def 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
218def packet_type_classify(ether):
219 try:
220 dot1q = ether[Dot1Q]
221 except:
222 dot1q = None
Dan Talayco08d9dfe2010-02-12 23:02:11 -0800223
Dan Talaycob66b1122010-02-10 22:38:49 -0800224 try:
225 ip = ether[IP]
226 except:
227 ip = None
Dan Talayco08d9dfe2010-02-12 23:02:11 -0800228
Dan Talaycob66b1122010-02-10 22:38:49 -0800229 try:
230 tcp = ether[TCP]
231 except:
232 tcp = None
Dan Talayco08d9dfe2010-02-12 23:02:11 -0800233
Dan Talaycob66b1122010-02-10 22:38:49 -0800234 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
243def packet_to_flow_match(packet, pkt_format="L2"):
244 """
245 Create a flow match that matches packet with the given wildcards
Dan Talaycob9cb5482010-02-09 15:23:12 -0800246
247 @param packet The packet to use as a flow template
Dan Talaycoa3b20182010-02-10 22:49:34 -0800248 @param pkt_format Currently only L2 is supported. Will indicate the
249 overall packet type for parsing
Dan Talaycob66b1122010-02-10 22:38:49 -0800250 @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 Talaycoa3b20182010-02-10 22:49:34 -0800254 @todo check min length of packet
255 @todo Check if packet is other than L2 format
256 @todo Implement ICMP and ARP fields
Dan Talaycob9cb5482010-02-09 15:23:12 -0800257 """
258
Dan Talaycob66b1122010-02-10 22:38:49 -0800259 #@todo check min length of packet
Dan Talaycob66b1122010-02-10 22:38:49 -0800260 if pkt_format.upper() != "L2":
Dan Talayco48370102010-03-03 15:17:33 -0800261 parse_logger.error("Only L2 supported for packet_to_flow")
Dan Talaycob66b1122010-02-10 22:38:49 -0800262 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