blob: 938076268d1eff1b830c28d6c41f8f0372feedd5 [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
23# These message types are subclassed
24msg_type_subclassed = [
25 OFPT_STATS_REQUEST,
26 OFPT_STATS_REPLY,
27 OFPT_ERROR
28]
29
30# Maps from sub-types to classes
31stats_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
40stats_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
49error_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
59msg_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
84def _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 Talaycob9cb5482010-02-09 15:23:12 -080096 sub_hdr = ofp_stats_request()
Dan Talaycob66b1122010-02-10 22:38:49 -080097 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 Talaycof75360a2010-02-05 22:22:54 -0800103 elif hdr.type == OFPT_STATS_REPLY:
Dan Talaycob9cb5482010-02-09 15:23:12 -0800104 sub_hdr = ofp_stats_reply()
Dan Talaycob66b1122010-02-10 22:38:49 -0800105 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 Talaycob9cb5482010-02-09 15:23:12 -0800111 elif hdr.type == OFPT_ERROR:
112 sub_hdr = ofp_error_msg()
Dan Talaycob66b1122010-02-10 22:38:49 -0800113 sub_hdr.unpack(binary_string[OFP_HEADER_BYTES:])
Dan Talaycob9cb5482010-02-09 15:23:12 -0800114 return error_to_class_map[sub_hdr.type]()
Dan Talaycof75360a2010-02-05 22:22:54 -0800115 else:
116 print "ERROR parsing packet to object"
117 return None
118
119def 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 Talaycof75360a2010-02-05 22:22:54 -0800127 @param raw If true, interpret the packet as an L2 packet. Not
128 yet supported.
Dan Talaycof75360a2010-02-05 22:22:54 -0800129 @return An object of some message class or None if fails
130
131 """
132
Dan Talaycob66b1122010-02-10 22:38:49 -0800133 #@todo verify that object type is really a message
Dan Talaycof75360a2010-02-05 22:22:54 -0800134 if raw:
135 print "raw packet message parsing not supported"
136 return None
137
138 obj = _of_message_to_object(binary_string)
Dan Talaycob66b1122010-02-10 22:38:49 -0800139 if obj:
Dan Talaycof75360a2010-02-05 22:22:54 -0800140 obj.unpack(binary_string)
141 return obj
142
Dan Talaycob9cb5482010-02-09 15:23:12 -0800143
144def 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 Talaycob66b1122010-02-10 22:38:49 -0800167map_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
188def parse_mac(mac_str):
Dan Talaycob9cb5482010-02-09 15:23:12 -0800189 """
Dan Talaycob66b1122010-02-10 22:38:49 -0800190 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
199def 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
215def 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
237def packet_to_flow_match(packet, pkt_format="L2"):
238 """
239 Create a flow match that matches packet with the given wildcards
Dan Talaycob9cb5482010-02-09 15:23:12 -0800240
241 @param packet The packet to use as a flow template
Dan Talaycob9cb5482010-02-09 15:23:12 -0800242
Dan Talaycob66b1122010-02-10 22:38:49 -0800243 @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 Talaycob9cb5482010-02-09 15:23:12 -0800248 """
249
Dan Talaycob66b1122010-02-10 22:38:49 -0800250 #@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