blob: 54b72269cab3c9921d6098cb347c14cb39a88f86 [file] [log] [blame]
Dan Talaycob66b1122010-02-10 22:38:49 -08001"""
2OpenFlow message parsing functions
3"""
4
5import sys
Dan Talayco235d7cb2010-03-12 10:03:18 -08006import logging
Dan Talaycof75360a2010-02-05 22:22:54 -08007from message import *
8from error import *
9from action import *
10from action_list import action_list
Dan Talaycob66b1122010-02-10 22:38:49 -080011from cstruct import *
12try:
Dan Talayco958f3b92010-03-12 21:58:57 -080013 import scapy.all as scapy
Dan Talaycob66b1122010-02-10 22:38:49 -080014except:
Dan Talaycod2ca1032010-03-10 14:40:26 -080015 try:
Dan Talayco958f3b92010-03-12 21:58:57 -080016 import scapy as scapy
Dan Talaycod2ca1032010-03-10 14:40:26 -080017 except:
18 sys.exit("Need to install scapy for packet parsing")
Dan Talaycof75360a2010-02-05 22:22:54 -080019
20"""
21of_message.py
22Contains wrapper functions and classes for the of_message namespace
23that are generated by hand. It includes the rest of the wrapper
24function information into the of_message namespace
25"""
26
Dan Talayco48370102010-03-03 15:17:33 -080027parse_logger = logging.getLogger("parse")
28#parse_logger.setLevel(logging.DEBUG)
Dan Talayco08d9dfe2010-02-12 23:02:11 -080029
Dan Talaycof75360a2010-02-05 22:22:54 -080030# These message types are subclassed
31msg_type_subclassed = [
32 OFPT_STATS_REQUEST,
33 OFPT_STATS_REPLY,
34 OFPT_ERROR
35]
36
37# Maps from sub-types to classes
38stats_reply_to_class_map = {
39 OFPST_DESC : desc_stats_reply,
40 OFPST_AGGREGATE : aggregate_stats_reply,
41 OFPST_FLOW : flow_stats_reply,
42 OFPST_TABLE : table_stats_reply,
43 OFPST_PORT : port_stats_reply,
44 OFPST_QUEUE : queue_stats_reply
45}
46
47stats_request_to_class_map = {
48 OFPST_DESC : desc_stats_request,
49 OFPST_AGGREGATE : aggregate_stats_request,
50 OFPST_FLOW : flow_stats_request,
51 OFPST_TABLE : table_stats_request,
52 OFPST_PORT : port_stats_request,
53 OFPST_QUEUE : queue_stats_request
54}
55
56error_to_class_map = {
57 OFPET_HELLO_FAILED : hello_failed_error_msg,
58 OFPET_BAD_REQUEST : bad_request_error_msg,
59 OFPET_BAD_ACTION : bad_action_error_msg,
60 OFPET_FLOW_MOD_FAILED : flow_mod_failed_error_msg,
61 OFPET_PORT_MOD_FAILED : port_mod_failed_error_msg,
62 OFPET_QUEUE_OP_FAILED : queue_op_failed_error_msg
63}
64
65# Map from header type value to the underlieing message class
66msg_type_to_class_map = {
67 OFPT_HELLO : hello,
68 OFPT_ERROR : error,
69 OFPT_ECHO_REQUEST : echo_request,
70 OFPT_ECHO_REPLY : echo_reply,
71 OFPT_VENDOR : vendor,
72 OFPT_FEATURES_REQUEST : features_request,
73 OFPT_FEATURES_REPLY : features_reply,
74 OFPT_GET_CONFIG_REQUEST : get_config_request,
75 OFPT_GET_CONFIG_REPLY : get_config_reply,
76 OFPT_SET_CONFIG : set_config,
77 OFPT_PACKET_IN : packet_in,
78 OFPT_FLOW_REMOVED : flow_removed,
79 OFPT_PORT_STATUS : port_status,
80 OFPT_PACKET_OUT : packet_out,
81 OFPT_FLOW_MOD : flow_mod,
82 OFPT_PORT_MOD : port_mod,
83 OFPT_STATS_REQUEST : stats_request,
84 OFPT_STATS_REPLY : stats_reply,
85 OFPT_BARRIER_REQUEST : barrier_request,
86 OFPT_BARRIER_REPLY : barrier_reply,
87 OFPT_QUEUE_GET_CONFIG_REQUEST : queue_get_config_request,
88 OFPT_QUEUE_GET_CONFIG_REPLY : queue_get_config_reply
89}
90
91def _of_message_to_object(binary_string):
92 """
93 Map a binary string to the corresponding class.
94
95 Appropriately resolves subclasses
96 """
97 hdr = ofp_header()
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]()
102 if hdr.type == OFPT_STATS_REQUEST:
Dan Talaycob9cb5482010-02-09 15:23:12 -0800103 sub_hdr = ofp_stats_request()
Dan Talaycob66b1122010-02-10 22:38:49 -0800104 sub_hdr.unpack(binary_string[OFP_HEADER_BYTES:])
105 try:
106 obj = stats_request_to_class_map[sub_hdr.type]()
107 except KeyError:
108 obj = None
109 return obj
Dan Talaycof75360a2010-02-05 22:22:54 -0800110 elif hdr.type == OFPT_STATS_REPLY:
Dan Talaycob9cb5482010-02-09 15:23:12 -0800111 sub_hdr = ofp_stats_reply()
Dan Talaycob66b1122010-02-10 22:38:49 -0800112 sub_hdr.unpack(binary_string[OFP_HEADER_BYTES:])
113 try:
114 obj = stats_reply_to_class_map[sub_hdr.type]()
115 except KeyError:
116 obj = None
117 return obj
Dan Talaycob9cb5482010-02-09 15:23:12 -0800118 elif hdr.type == OFPT_ERROR:
119 sub_hdr = ofp_error_msg()
Dan Talaycob66b1122010-02-10 22:38:49 -0800120 sub_hdr.unpack(binary_string[OFP_HEADER_BYTES:])
Dan Talaycob9cb5482010-02-09 15:23:12 -0800121 return error_to_class_map[sub_hdr.type]()
Dan Talaycof75360a2010-02-05 22:22:54 -0800122 else:
Dan Talayco48370102010-03-03 15:17:33 -0800123 parse_logger.error("Cannot parse pkt to message")
Dan Talaycof75360a2010-02-05 22:22:54 -0800124 return None
125
126def 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 Talaycof75360a2010-02-05 22:22:54 -0800134 @param raw If true, interpret the packet as an L2 packet. Not
135 yet supported.
Dan Talaycof75360a2010-02-05 22:22:54 -0800136 @return An object of some message class or None if fails
Dan Talayco08d9dfe2010-02-12 23:02:11 -0800137 Note that any data beyond that parsed is not returned
Dan Talaycof75360a2010-02-05 22:22:54 -0800138
139 """
140
141 if raw:
Dan Talayco48370102010-03-03 15:17:33 -0800142 parse_logger.error("raw packet message parsing not supported")
Dan Talaycof75360a2010-02-05 22:22:54 -0800143 return None
144
145 obj = _of_message_to_object(binary_string)
Dan Talaycob66b1122010-02-10 22:38:49 -0800146 if obj:
Dan Talaycof75360a2010-02-05 22:22:54 -0800147 obj.unpack(binary_string)
148 return obj
149
Dan Talaycob9cb5482010-02-09 15:23:12 -0800150
151def 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 Talayco48370102010-03-03 15:17:33 -0800166 parse_logger.error("raw packet message parsing not supported")
Dan Talaycob9cb5482010-02-09 15:23:12 -0800167 return None
168
169 hdr = ofp_header()
170 hdr.unpack(binary_string)
171
172 return hdr
173
Dan Talaycob66b1122010-02-10 22:38:49 -0800174map_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
195def parse_mac(mac_str):
Dan Talaycob9cb5482010-02-09 15:23:12 -0800196 """
Dan Talaycob66b1122010-02-10 22:38:49 -0800197 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 """
204 return map(lambda val:eval("0x" + val), mac_str.split(":"))
205
206def 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 """
215 array = map(lambda val:eval(val),ip_str.split("."))
216 val = 0
217 for a in array:
218 val <<= 8
219 val += a
220 return val
221
222def packet_type_classify(ether):
223 try:
Dan Talayco958f3b92010-03-12 21:58:57 -0800224 dot1q = ether[scapy.Dot1Q]
Dan Talaycob66b1122010-02-10 22:38:49 -0800225 except:
226 dot1q = None
Dan Talayco08d9dfe2010-02-12 23:02:11 -0800227
Dan Talaycob66b1122010-02-10 22:38:49 -0800228 try:
Dan Talayco958f3b92010-03-12 21:58:57 -0800229 ip = ether[scapy.IP]
Dan Talaycob66b1122010-02-10 22:38:49 -0800230 except:
231 ip = None
Dan Talayco08d9dfe2010-02-12 23:02:11 -0800232
Dan Talaycob66b1122010-02-10 22:38:49 -0800233 try:
Dan Talayco958f3b92010-03-12 21:58:57 -0800234 tcp = ether[scapy.TCP]
Dan Talaycob66b1122010-02-10 22:38:49 -0800235 except:
236 tcp = None
Dan Talayco08d9dfe2010-02-12 23:02:11 -0800237
Dan Talaycob66b1122010-02-10 22:38:49 -0800238 try:
Dan Talayco958f3b92010-03-12 21:58:57 -0800239 udp = ether[scapy.UDP]
Dan Talaycob66b1122010-02-10 22:38:49 -0800240 except:
241 udp = None
242
Tatsuya Yabeb8fb3c32010-06-14 15:48:36 -0700243 try:
244 icmp = ether[scapy.ICMP]
245 except:
246 icmp = None
247
248 # @todo arp is not yet supported
249 arp = None
Dan Talaycob66b1122010-02-10 22:38:49 -0800250 return (dot1q, ip, tcp, udp, icmp, arp)
251
252def packet_to_flow_match(packet, pkt_format="L2"):
253 """
254 Create a flow match that matches packet with the given wildcards
Dan Talaycob9cb5482010-02-09 15:23:12 -0800255
256 @param packet The packet to use as a flow template
Dan Talaycoa3b20182010-02-10 22:49:34 -0800257 @param pkt_format Currently only L2 is supported. Will indicate the
258 overall packet type for parsing
Dan Talaycob66b1122010-02-10 22:38:49 -0800259 @return An ofp_match object if successful. None if format is not
260 recognized. The wildcards of the match will be cleared for the
261 values extracted from the packet.
262
Dan Talaycoa3b20182010-02-10 22:49:34 -0800263 @todo check min length of packet
264 @todo Check if packet is other than L2 format
265 @todo Implement ICMP and ARP fields
Dan Talaycob9cb5482010-02-09 15:23:12 -0800266 """
267
Dan Talaycob66b1122010-02-10 22:38:49 -0800268 #@todo check min length of packet
Dan Talaycob66b1122010-02-10 22:38:49 -0800269 if pkt_format.upper() != "L2":
Dan Talayco48370102010-03-03 15:17:33 -0800270 parse_logger.error("Only L2 supported for packet_to_flow")
Dan Talaycob66b1122010-02-10 22:38:49 -0800271 return None
272
Dan Talaycocb6b5d72010-03-10 13:59:33 -0800273 if type(packet) == type(""):
Dan Talayco958f3b92010-03-12 21:58:57 -0800274 ether = scapy.Ether(packet)
Dan Talaycocb6b5d72010-03-10 13:59:33 -0800275 else:
276 ether = packet
277
Dan Talaycob66b1122010-02-10 22:38:49 -0800278 # For now, assume ether IP packet and ignore wildcards
Dan Talaycocb6b5d72010-03-10 13:59:33 -0800279 try:
280 (dot1q, ip, tcp, udp, icmp, arp) = packet_type_classify(ether)
281 except:
282 parse_logger.error("packet_to_flow_match: Classify error")
283 return None
Dan Talaycob66b1122010-02-10 22:38:49 -0800284
285 match = ofp_match()
286 match.wildcards = OFPFW_ALL
287 #@todo Check if packet is other than L2 format
288 match.dl_dst = parse_mac(ether.dst)
289 match.wildcards &= ~OFPFW_DL_DST
290 match.dl_src = parse_mac(ether.src)
291 match.wildcards &= ~OFPFW_DL_SRC
292 match.dl_type = ether.type
293 match.wildcards &= ~OFPFW_DL_TYPE
294
295 if dot1q:
296 match.dl_vlan = dot1q.vlan
Dan Talaycob66b1122010-02-10 22:38:49 -0800297 match.dl_vlan_pcp = dot1q.prio
Tatsuya Yabeb8fb3c32010-06-14 15:48:36 -0700298 else:
299 match.dl_vlan = OFP_VLAN_NONE
300 match.dl_vlan_pcp = 0
301 match.wildcards &= ~OFPFW_DL_VLAN
302 match.wildcards &= ~OFPFW_DL_VLAN_PCP
Dan Talaycob66b1122010-02-10 22:38:49 -0800303
304 if ip:
305 match.nw_src = parse_ip(ip.src)
306 match.wildcards &= ~OFPFW_NW_SRC_MASK
307 match.nw_dst = parse_ip(ip.dst)
308 match.wildcards &= ~OFPFW_NW_DST_MASK
309 match.nw_tos = ip.tos
310 match.wildcards &= ~OFPFW_NW_TOS
311
312 if tcp:
313 match.tp_src = tcp.sport
314 match.wildcards &= ~OFPFW_TP_SRC
315 match.tp_dst = tcp.dport
316 match.wildcards &= ~OFPFW_TP_DST
317
Tatsuya Yabeb8fb3c32010-06-14 15:48:36 -0700318 if icmp:
319 match.nw_proto = 1
320 match.tp_src = icmp.type
321 match.tp_dst = icmp.code
322
323 #@todo Implement ARP fields
Dan Talaycob66b1122010-02-10 22:38:49 -0800324
325 return match